package cn.getech.data.development.service.impl;

import cn.getech.data.development.config.properties.DataDevelopmentConfig;
import cn.getech.data.development.config.properties.RangerConfig;
import cn.getech.data.development.constant.*;
import cn.getech.data.development.dto.TableAndFieldDto;
import cn.getech.data.development.dto.TableFieldDto;
import cn.getech.data.development.entity.*;
import cn.getech.data.development.entity.dataMap.DataApplication;
import cn.getech.data.development.entity.permission.TableFieldInfoPermission;
import cn.getech.data.development.entity.permission.TableInfoPermission;
import cn.getech.data.development.entity.permission.TenantTableFieldInfoPermission;
import cn.getech.data.development.entity.permission.TenantTableInfoPermission;
import cn.getech.data.development.entity.ranger.RangerGroup;
import cn.getech.data.development.mapper.*;
import cn.getech.data.development.model.dto.*;
import cn.getech.data.development.model.ranger.*;
import cn.getech.data.development.service.RangerDataService;
import cn.getech.data.development.service.TableInfoService;
import cn.getech.data.development.service.UserRangerPolicyRelService;
import cn.getech.data.development.utils.ranger.RangerRestApi;
import cn.getech.data.development.utils.ranger.RangerRestUtil;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.system.center.entity.*;
import cn.getech.system.center.mapper.*;
import cn.getech.system.center.model.dto.RoleRowAndRoleDataPermissionDto;
import cn.getech.system.center.model.dto.RoleRowDataPermissionDto;
import cn.getech.system.center.model.dto.SysUserDto;
import cn.getech.system.center.model.req.role.RoleUsers;
import cn.getech.system.center.service.SysUserRoleService;
import cn.getech.system.center.service.SysUserService;
import cn.getech.system.center.utils.RoleUtils;
import cn.getech.system.center.utils.ShiroUtils;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.ranger.plugin.util.RangerRESTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RangerDataServiceImpl implements RangerDataService {

    private List<String> sqlTypeList = Arrays.asList(
            TableDataType.INT.getName(),
            TableDataType.FLOAT.getName(),
            TableDataType.DOUBLE.getName(),
            TableDataType.BIGINT.getName(),
            TableDataType.TIMESTAMP.getName()
    );
    @Autowired
    private RangerConfig rangerConfig;
    @Autowired
    private TableInfoService tableInfoService;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private DataDevelopmentConfig dataDevelopmentConfig;

    @Autowired
    private UserRangerPolicyRelService userRangerPolicyRelService;

    @Autowired
    private RoleRowDataPermissionConfigMapper roleRowDataPermissionConfigMapper;

    @Autowired
    private RoleDataPermissionConfigMapper roleDataPermissionConfigMapper;

    @Autowired
    private TableFieldRulesMapper tableFieldRulesMapper;

    @Autowired
    private ThreadPoolTaskExecutor myExecutor;

    @Autowired
    private ProcTableMapper procTableMapper;

    @Autowired
    private TableInfoMapper tableInfoMapper;

    @Autowired
    private ProcUserMapper procUserMapper;

    @Autowired
    private AllPermissionConfigMapper allPermissionConfigMapper;

    @Autowired
    private TableFieldInfoMapper tableFieldInfoMapper;

    @Autowired
    private ProcInfoMapper procInfoMapper;

    @Autowired
    private TableFieldMaskRulesMapper tableFieldMaskRulesMapper;

    @Autowired
    private CusMaskUdfMapper cusMaskUdfMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysTenantUserMapper sysTenantUserMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 项目启动时，初始化ranger连接
     */
    @Override
    public void init() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
                } catch (Exception e) {
                    log.info("初始化对应的ranger是否登录");
                }
            }
        }).start();
    }

    @Override
    public RangerService getServiceByName(String name) {
        if (StringUtils.isEmpty(name)) {
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_GET_SERVICE_BY_NAME_PARAM_NAME.getMessage());
        }
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.GET_SERVICE_BY_NAME + name;
        WebResource webResource = client.resource(url);
        RangerService entity = null;
        ClientResponse clientResponse = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .get(ClientResponse.class);
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerService.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_GET_SERVICE_BY_NAME.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_GET_SERVICE_BY_NAME.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        return entity;
    }

    @Override
    public RangerService addServiceByService(RangerService rangerService) {
        //验证参数
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.POST_ADD_SERVICE;
        WebResource webResource = client.resource(url);
        RangerService entity = null;
        ClientResponse clientResponse = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .post(ClientResponse.class, JSONObject.toJSONString(rangerService));
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerService.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_POST_SERVICE.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_POST_SERVICE.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        return entity;
    }

    @Override
    public RangerService updateServiceByServiceName(RangerService rangerService, String name) {
        if (StringUtils.isEmpty(name)) {
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_PUT_SERVICE_BY_NAME_PARAM_NAME.getMessage());
        }
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.PUT_UPDATE_SERVICE + name;
        WebResource webResource = client.resource(url);
        RangerService entity = null;
        ClientResponse clientResponse = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .put(ClientResponse.class, JSONObject.toJSONString(rangerService));
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerService.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_PUT_SERVICE.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_PUT_SERVICE.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        return entity;
    }

    @Override
    public void deleteByServiceName(String name) {
        if (StringUtils.isEmpty(name)) {
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_SERVICE_BY_NAME_PARAM_NAME.getMessage());
        }
        //请求删除服务的接口
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.DELETE_SERVICE + name;
        WebResource webResource = client.resource(url);
        try {
            webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .delete();
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_SERVICE_BY_NAME.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_SERVICE_BY_NAME.getMessage());
        }
    }
    private void updatePolicy(Set<String> user, Integer policeId, RoleUsers roleUsers, Integer id){
        RangerPolicy rangerPolicy = getRangerPolicy(policeId);
        if (rangerPolicy != null) {
            List<RangerPolicyItem> policyItems = rangerPolicy.getPolicyItems();
            user.addAll(roleUsers.getUsers());
            policyItems.get(0).setUsers(user);
            rangerPolicy.setPolicyItems(policyItems);
            putRangerPolicy(rangerPolicy, policeId);
        }else{
            log.warn("策略不存在policeId:{}",policeId);
            userRangerPolicyRelService.removeById(id);
        }
    }



    @Override
    public void updateRangerPolicy(List<RoleUsers> roleList) {
        if (roleList != null) {
            myExecutor.execute(() -> {
                for (RoleUsers roleUsers : roleList) {
                    Long roleId = roleUsers.getRoleId();
                    if (roleId == 1) {//超管的策略

                        Set<String> user = Sets.newHashSet();
                        user.add("hdfs");
                        try {
                            Integer policeId = RangerConfig.POLICE_ID;
                            updatePolicy(user, policeId, roleUsers, -1);
                            log.info("super user list:{}", JSONObject.toJSONString(roleUsers));
                            log.info("update super role success   policeId:{},",policeId);
                        } catch (Exception e) {
                            log.warn("修改超管策略策略异常",e);
                        }
                    } else {
                        List<UserRangerPolicyRel> list = userRangerPolicyRelService.list(
                                new QueryWrapper<UserRangerPolicyRel>().eq("role_id", roleId));
                        if (CollectionUtil.isNotEmpty(list)) {
                            Integer num = list.size();
                            for (UserRangerPolicyRel userRangerPolicyRel : list) {
                                try {
                                    Integer policeId = userRangerPolicyRel.getPolicyId();
                                    Set<String> users = Sets.newHashSet();
                                    updatePolicy(users, policeId, roleUsers,userRangerPolicyRel.getId());
                                    log.info("修改角色后，待修改策略个数:{}", num);
                                    num--;
                                    //Thread.sleep(1000 );
                                } catch (Exception e) {
                                    log.warn("修改策略异常", e);
                                }
                            }
                        }
                    }
                }
            });
        }
    }

    @Override
    public synchronized RangerPolicy addRangerPolicy(RangerPolicy rangerPolicy) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.POST_POLICY;
        WebResource webResource = client.resource(url);
        RangerPolicy entity = null;
        ClientResponse clientResponse = null;
        String paramJson = JSONObject.toJSONString(rangerPolicy);
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .post(ClientResponse.class, paramJson);
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerPolicy.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_POST_POLICY.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_POST_POLICY.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        if (null == entity) {
            log.error("生成策略失败：{}", paramJson);
        }
        return entity;
    }

    @Override
    public RangerPolicy getRangerPolicy(int id) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.GET_POLICY + id;
        WebResource webResource = client.resource(url);
        RangerPolicy entity = null;
        ClientResponse clientResponse = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .get(ClientResponse.class);
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerPolicy.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_GET_POLICY.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_GET_POLICY.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        return entity;
    }

    @Override
    public synchronized RangerPolicy putRangerPolicy(RangerPolicy rangerPolicy, int id) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.POST_POLICY_APPLY + id;
        WebResource webResource = client.resource(url);
        RangerPolicy entity = null;
        ClientResponse clientResponse = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .put(ClientResponse.class, JSONObject.toJSONString(rangerPolicy));
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerPolicy.class);
            }else{
                log.warn("修改策略失败:\n {}",JSONObject.toJSONString(rangerPolicy));
            }
        } catch (Exception e) {
            log.error("修改策略失败:\n{}",JSONObject.toJSONString(rangerPolicy));
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_POST_POLICY_APPLY.getMessage() + ",error：{}", e.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        return entity;
    }

    @Override
    public synchronized void deleteRangerPolicy(int id) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.DELET_POLICY + id;
        WebResource webResource = client.resource(url);
        try {
            webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .delete();
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_POLICY.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_POLICY.getMessage());
        }
    }

    @Override
    public synchronized RangerUser addRangerUser(RangerUser rangerUser) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.POST_USER;
        WebResource webResource = client.resource(url);
        RangerUser entity = null;
        ClientResponse clientResponse = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .post(ClientResponse.class, JSONObject.toJSONString(rangerUser));
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerUser.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_POST_USER.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_POST_USER.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        if (entity == null) {
            log.warn("添加用户失败:{}",JSONObject.toJSONString(rangerUser));
        }
        return entity;
    }

    @Override
    public RangerUser getRangerUser(String name) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.GET_USER_BY_NAME + name;
        WebResource webResource = client.resource(url);
        RangerUser entity = null;
        ClientResponse clientResponse = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .get(ClientResponse.class);
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerUser.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_GET_USER.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_GET_USER.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        return entity;
    }

    @Override
    public synchronized  void deleteRangerUser(String userName) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        //强制删除
        String url = rangerConfig.getResturl() + RangerRestApi.DELETE_USER_BY_NAME + userName + "?forceDelete=true";
        WebResource webResource = client.resource(url);
        try {
            webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .delete();
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_USER.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_USER.getMessage());
        }
    }

    @Override
    public List<RangerPolicyDto> listAllUserAndTablesPression(Integer type, List<UserAndTableDto> userAndTableDtos) {
        //判断对应的表和对应的用户，并且生成对应的权限集合，并且生成策略。并且保存到ranger中去
        if (CollectionUtil.isEmpty(userAndTableDtos)) {
            return null;
        }
        //查询对应的表和数据
        List<RangerPolicyDto> rangerPolicyDtos = new ArrayList<>();
        for (UserAndTableDto userAndTableDto : userAndTableDtos) {
            RangerPolicyDto rangerPolicyDto = new RangerPolicyDto();
            if (null == userAndTableDto.getUserId()) {
                continue;
            }
            rangerPolicyDto.setUserId(userAndTableDto.getUserId());
            rangerPolicyDto.setRangerUserName(dataDevelopmentConfig.getUserPreffix() + userAndTableDto.getUserId());
            if (CollectionUtil.isEmpty(userAndTableDto.getAllTableAndFieldDtoList())) {
                packExcludeAllData(rangerPolicyDto, userAndTableDto.getUserId());
                rangerPolicyDtos.add(rangerPolicyDto);
                continue;
            }
            //查询到当前用户对应的角色配置的权限
            SysUserDto sysUserDto = sysUserRoleService.queryUserInfoByUserId(userAndTableDto.getUserId(), ShiroUtils.getTenantId());
            if (null == sysUserDto) {
                throw new RRException(DataDevelopmentBizExceptionEnum.USER_NOTFOUND.getMessage(), DataDevelopmentBizExceptionEnum.USER_NOTFOUND.getCode());
            }
            if (CollectionUtil.isEmpty(sysUserDto.getRoleIdList())) {
                throw new RRException(DataDevelopmentBizExceptionEnum.ROLE_NOTFOUND.getMessage(), DataDevelopmentBizExceptionEnum.ROLE_NOTFOUND.getCode());
            }
            //初始化权限
            Set<Integer> allDbIds = new HashSet<>();
            Set<Integer> alltableIds = new HashSet<>();
            Set<Integer> allConlumIds = new HashSet<>();
            Set<RoleRowDataPermissionDto> roleRowDataPermissionDtos = new HashSet<>();
            tableInfoService.initDbAndTableAndConlumIds(allDbIds, alltableIds, allConlumIds, roleRowDataPermissionDtos, sysUserDto);

            //对应的用户的库、表、字段、行的权限
            initPressionAndRangerPolicyDto(type, allDbIds, alltableIds, allConlumIds, rangerPolicyDto, userAndTableDto);

            rangerPolicyDtos.add(rangerPolicyDto);

        }

        Set<Integer> tableIds = new HashSet<>();
        Set<Integer> tempTableIds = new HashSet<>();
        for (UserAndTableDto userAndTableDto : userAndTableDtos) {
            if(null != userAndTableDto && CollectionUtil.isNotEmpty(userAndTableDto.getAllTableAndFieldDtoList())){
                for (TableAndFieldDto tableAndFieldDto : userAndTableDto.getAllTableAndFieldDtoList()) {
                    tempTableIds.add(tableAndFieldDto.getTableInfoId());
                }
            }
            if(CollectionUtil.isNotEmpty(tempTableIds)){
                tableIds.addAll(tempTableIds);
            }
            tempTableIds.clear();
        }
        if(CollectionUtil.isEmpty(tableIds)){
            return rangerPolicyDtos;
        }
        List<Integer> newTableIds = new ArrayList<>(tableIds);
        //行规则特殊，自己生成
        if (RangerPolicyType.ROW_LEVEL_FILTER.getId() == type || 3 == type) {
            initAllHaveRowRulesTablePolicyByTableIds(newTableIds);
        }
        //脱敏规则特殊，自己生成
        if (RangerPolicyType.MASKING.getId() == type || 3 == type) {
            initAllHaveMaskRulesTablePolicyByTableIds(newTableIds);
        }
        return rangerPolicyDtos;
    }

    private void initAllHaveMaskRulesTablePolicyByTableIds(List<Integer> tableIds){
        if(CollectionUtil.isEmpty(tableIds)){
            return;
        }
        //查询到所有配置了字段脱敏规则的表信息和规则信息（开启的表以及其字段）
        List<TableFieldAndTableDto> tableFieldAndTableDtos = tableInfoMapper.seleAllTableInfoMaskOpenByTableIds(tableIds);
        if(CollectionUtil.isEmpty(tableFieldAndTableDtos)){
            return;
        }
        //按照表进行分组
        Map<Integer, List<TableFieldAndTableDto>> tableListMap = tableFieldAndTableDtos.stream().collect(Collectors.groupingBy(TableFieldAndTableDto::getTableId));
        for (Map.Entry<Integer, List<TableFieldAndTableDto>> entry : tableListMap.entrySet()) {
            if(CollectionUtil.isNotEmpty(entry.getValue())){
                myExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        for(TableFieldAndTableDto tableFieldAndTableDto : tableFieldAndTableDtos){
                            saveRangerPolicyMaskingByTableFieldId(tableFieldAndTableDto.getId());
                        }
                    }
                });
            }
        }
    }

    /***
     * 初始化所有的行规则策略数据
     */
    @Override
    public void initAllHaveMaskRulesTablePolicy() {
        //查询到所有配置了字段脱敏规则的表信息和规则信息（开启的表以及其字段）
        List<TableFieldAndTableDto> tableFieldAndTableDtos = tableInfoMapper.seleAllTableInfoMaskOpen();
        if(CollectionUtil.isEmpty(tableFieldAndTableDtos)){
            return;
        }
        //按照表进行分组
        Map<Integer, List<TableFieldAndTableDto>> tableListMap = tableFieldAndTableDtos.stream().collect(Collectors.groupingBy(TableFieldAndTableDto::getTableId));
        for (Map.Entry<Integer, List<TableFieldAndTableDto>> entry : tableListMap.entrySet()) {
            if(CollectionUtil.isNotEmpty(entry.getValue())){
                myExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        for(TableFieldAndTableDto tableFieldAndTableDto : tableFieldAndTableDtos){
                            saveRangerPolicyMaskingByTableFieldId(tableFieldAndTableDto.getId());
                        }
                    }
                });
            }
        }
    }

    @Override
    public void deleteRangerByRangerUserName(String userName) {
        if(StringUtils.isEmpty(userName)){
            return;
        }
        String[] splitUserName = userName.split("_");
        if(null == splitUserName || splitUserName.length <= 1){
            return;
        }
        //删除信息
        //查询到对应的关联关系
        List<UserRangerPolicyRel> listAllUserRangerPolicyRels = userRangerPolicyRelService.list(new QueryWrapper<UserRangerPolicyRel>()
                .eq("user_id", splitUserName[1])
                //行规则特殊，自己创建
                .eq("policy_type", RangerPolicyType.ACCESS.getId()));
        if (CollectionUtil.isNotEmpty(listAllUserRangerPolicyRels)) {
            for (UserRangerPolicyRel listAllUserRangerPolicyRel : listAllUserRangerPolicyRels) {
                try {
                    deleteRangerPolicy(listAllUserRangerPolicyRel.getPolicyId());
                } catch (Exception e) {
                    log.error("删除ranger-策略异常！", e.getMessage());
                }
                userRangerPolicyRelService.removeById(listAllUserRangerPolicyRel.getId());
            }
        }

    }

    @Override
    public void genteratorRangerPolicy(List<Long> userIds) {

        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }
        //组装对应的用户信息表信息
        List<UserAndTableDto> userAndTableDtos = tableInfoService.packUserAndTableData(userIds);
        if (CollectionUtil.isEmpty(userAndTableDtos)) {
            return;
        }
        //拼装所有的策略
        List<RangerPolicyDto> rangerPolicyDtos = listAllUserAndTablesPression(3, userAndTableDtos);
        if (CollectionUtil.isEmpty(rangerPolicyDtos)) {
            return;
        }

        //先删除，再新增
        for (RangerPolicyDto rangerPolicyDto : rangerPolicyDtos) {
            //查询到对应的关联关系
            List<UserRangerPolicyRel> listAllUserRangerPolicyRels = userRangerPolicyRelService.list(new QueryWrapper<UserRangerPolicyRel>()
                    .eq("user_id", rangerPolicyDto.getUserId())
                    //行规则特殊，自己创建
                    .eq("policy_type", RangerPolicyType.ACCESS.getId()));
            if (CollectionUtil.isNotEmpty(listAllUserRangerPolicyRels)) {
                for (UserRangerPolicyRel listAllUserRangerPolicyRel : listAllUserRangerPolicyRels) {
                    try {
                        deleteRangerPolicy(listAllUserRangerPolicyRel.getPolicyId());
                    } catch (Exception e) {
                        log.error("删除ranger-策略异常！", e.getMessage());
                    }
                    userRangerPolicyRelService.removeById(listAllUserRangerPolicyRel.getId());
                }
            }
        }

        //生成对应的策略，并且保存关联信息
        for (RangerPolicyDto rangerPolicyDto : rangerPolicyDtos) {
            if (null != rangerPolicyDto && CollectionUtil.isNotEmpty(rangerPolicyDto.getRangerPolicies())) {
                List<RangerPolicy> rangerPolicies = rangerPolicyDto.getRangerPolicies();
                for (int i = 0; i <= rangerPolicies.size() / 10; i++) {
                    List<RangerPolicy> collect = rangerPolicies.stream().skip(i * 10).limit(10).collect(Collectors.toList());
                    myExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            List<UserRangerPolicyRel> userRangerPolicyRels = new ArrayList<>();
                            for (RangerPolicy rangerPolicy : collect) {
                                RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicy);
                                if (null != rangerPolicy1) {
                                    UserRangerPolicyRel userRangerPolicyRel = new UserRangerPolicyRel();
                                    userRangerPolicyRel.setUserId(rangerPolicyDto.getUserId());
                                    userRangerPolicyRel.setPolicyId(rangerPolicy1.getId());
                                    userRangerPolicyRel.setCreateTime(DateTime.now());
                                    userRangerPolicyRel.setPolicyType(rangerPolicy1.getPolicyType());
                                    String[] split = rangerPolicy.getName().split("_");
                                    if (null != split && split.length >= 3) {
                                        userRangerPolicyRel.setTableId(Integer.valueOf(split[2]));
                                    }
                                    userRangerPolicyRels.add(userRangerPolicyRel);
                                }
                            }
                            //批量保存关联信息
                            if (CollectionUtil.isNotEmpty(userRangerPolicyRels)) {
                                userRangerPolicyRelService.saveBatch(userRangerPolicyRels);
                            }
                        }
                    });
                }
            }
        }
    }


    private void initAllHaveRowRulesTablePolicyByTableIds(List<Integer> tableIds){
        if(CollectionUtil.isEmpty(tableIds)){
            return;
        }
//        //查询到所有配置了行规则的
//        List<RoleRowAndRoleDataPermissionDto> roleRowAndRoleDataPermissionDtos = roleRowDataPermissionConfigMapper.selectAllRowPermissionByTableIds(tableIds);
//        if (CollectionUtil.isEmpty(roleRowAndRoleDataPermissionDtos)) {
//            return;
//        }
//        //按照表进行分组创建行规则策略
//        Map<Integer, List<RoleRowAndRoleDataPermissionDto>> tableMap = roleRowAndRoleDataPermissionDtos.stream().collect(Collectors.groupingBy(RoleRowAndRoleDataPermissionDto::getTableId));
//        List<Integer> tableList = new ArrayList<>();
//        for (Map.Entry<Integer, List<RoleRowAndRoleDataPermissionDto>> entry : tableMap.entrySet()) {
//            tableList.add(entry.getKey());
//        }

        //生成对应的策略
//        if (CollectionUtil.isNotEmpty(tableList)) {
//            for (int i = 0; i < tableList.size() / 10; i++) {
//                List<Integer> tempTableList = tableList.stream().skip(i * 10).limit(10).collect(Collectors.toList());
//                if (CollectionUtil.isNotEmpty(tempTableList)) {
//                    myExecutor.execute(new Runnable() {
//                        @Override
//                        public void run() {
//                            for (Integer tableId : tempTableList) {
//                                try {
//                                    saveRangerPolicy(tableId);
//                                } catch (Exception e) {
//                                    log.error("生成表：{}，行规则失败！error:{}", tableId, e.getMessage());
//                                }
//                            }
//                        }
//                    });
//                }
//            }
//        }
        //以上逻辑已经废弃，新的逻辑只需要生成对应的策略
        for (Integer tableId:tableIds) {
            myExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                saveRangerPolicy(tableId);
                            } catch (Exception e) {
                                log.error("生成表：{}，行规则失败！error:{}", tableId, e.getMessage());
                            }
                        }
                    });
        }
    }

    /***
     * 初始化所有配置行规则的策略
     * @return
     */
    @Override
    public void initAllHaveRowRulesTablePolicy() {
        //查询到所有配置了行规则的
        List<RoleRowAndRoleDataPermissionDto> roleRowAndRoleDataPermissionDtos = roleRowDataPermissionConfigMapper.selectAllRowPermission();
        if (CollectionUtil.isEmpty(roleRowAndRoleDataPermissionDtos)) {
            return;
        }
        //按照表进行分组创建行规则策略
        Map<Integer, List<RoleRowAndRoleDataPermissionDto>> tableMap = roleRowAndRoleDataPermissionDtos.stream().collect(Collectors.groupingBy(RoleRowAndRoleDataPermissionDto::getTableId));
        List<Integer> tableList = new ArrayList<>();
        for (Map.Entry<Integer, List<RoleRowAndRoleDataPermissionDto>> entry : tableMap.entrySet()) {
            tableList.add(entry.getKey());
        }

        //生成对应的策略
        if (CollectionUtil.isNotEmpty(tableList)) {
            for (int i = 0; i < tableList.size() / 10; i++) {
                List<Integer> tempTableList = tableList.stream().skip(i * 10).limit(10).collect(Collectors.toList());
                if (CollectionUtil.isNotEmpty(tempTableList)) {
                    myExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            for (Integer tableId : tempTableList) {
                                try {
                                    saveRangerPolicy(tableId);
                                } catch (Exception e) {
                                    log.error("生成表：{}，行规则失败！error:{}", tableId, e.getMessage());
                                }
                            }
                        }
                    });
                }
            }
        }

    }


    public void deleteRangerPolicy(TableFieldRulesDto tableFieldRulesDto) {
        //查询到这个规则关联的角色信息
        List<RoleRowDataPermissionConfig> listRoleDataConfigList = roleRowDataPermissionConfigMapper.selectList(new QueryWrapper<RoleRowDataPermissionConfig>()
                .eq("table_field_rules_id", tableFieldRulesDto.getId()));
        if (CollectionUtil.isEmpty(listRoleDataConfigList)) {
            return;
        }
        List<UserRangerPolicyRel> list = userRangerPolicyRelService.list(new QueryWrapper<UserRangerPolicyRel>().eq("policy_type", RangerPolicyType.ROW_LEVEL_FILTER.getId())
                .eq("table_id", tableFieldRulesDto.getTableId()));
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        for (UserRangerPolicyRel userRangerPolicyRel : list) {
            try {
                deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
            } catch (Exception e) {
                log.error("删除策略异常：error:{}", e.getMessage());
            }
            userRangerPolicyRelService.removeById(userRangerPolicyRel.getId());
        }
    }

    @Override
    public void saveOrUpdateRangerPolicy(TableFieldRules tableFieldRules) {
        if (null == tableFieldRules || null == tableFieldRules.getId() || null == tableFieldRules.getTableId()) {
            return;
        }
        //直接创建对应的表的行规则权限
        saveRangerPolicy(tableFieldRules.getTableId());


//
//        List<RoleRowDataPermissionConfig> listRoleDataConfigList = roleRowDataPermissionConfigMapper.selectList(new QueryWrapper<RoleRowDataPermissionConfig>()
//                .eq("table_field_rules_id", tableFieldRules.getId()));
//        if (CollectionUtil.isEmpty(listRoleDataConfigList)) {
//            return;
//        }
//        //编辑，才会修改
//        List<UserRangerPolicyRel> list = userRangerPolicyRelService.list(new QueryWrapper<UserRangerPolicyRel>().eq("policy_type", RangerPolicyType.ROW_LEVEL_FILTER.getId())
//                .eq("table_id", tableFieldRules.getTableId()));
//        if (CollectionUtil.isEmpty(list)) {
//            return;
//        }
//        //拼接对应的sql
//        RoleRowDataPermissionDto roleRowDataPermissionDto1 = tableFieldRulesMapper.selectRoleRowDataPermissionDtoById(tableFieldRules.getId());
//        if (null == roleRowDataPermissionDto1) {
//            return;
//        }
//        List<RoleRowDataPermissionDto> restList = Arrays.asList(roleRowDataPermissionDto1);
//        String sql = pingSql(restList);
//        if (StringUtils.isEmpty(sql)) {
//            return;
//        }
//        //更新所有的策略
//        for (UserRangerPolicyRel userRangerPolicyRel : list) {
//            RangerPolicy rangerPolicy = getRangerPolicy(userRangerPolicyRel.getPolicyId());
//            if (null != rangerPolicy) {
//                List<RangerRowFilterPolicyItem> rowFilterPolicyItems = rangerPolicy.getRowFilterPolicyItems();
//                if (CollectionUtil.isNotEmpty(rowFilterPolicyItems)) {
//                    rowFilterPolicyItems.get(0).getRowFilterInfo().setFilterExpr(sql);
//                }
//                putRangerPolicy(rangerPolicy, userRangerPolicyRel.getPolicyId());
//            }
//        }
    }

    @Override
    public void saveRangerPolicy(Integer tableId) {
        if (null == tableId) {
            return;
        }
        //获取到所有能够看到这个表的用户信息
        List<Long> userIds = tableInfoService.packUserIdByTableId(tableId);
        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }
        //此项目中一个用户对应一个角色 === 查询到所有的
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleService.queryUserAndRoleBy(userIds);
        if (CollectionUtil.isEmpty(sysUserRoleEntities)) {
            return;
        }

        //不管有没有策略，还是的先删除掉
        //先查询是否存在这个表的行规则策略，如果没有则直接删除
        UserRangerPolicyRel userRangerPolicyRel = userRangerPolicyRelService.limitOneByTableIdAndRowLevel(tableId);
        //先删除 再新增
        if (null != userRangerPolicyRel) {
            userRangerPolicyRelService.deleteById(userRangerPolicyRel.getId());
            deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
        }


        //按照角色进行分组
        Map<Long, List<SysUserRoleEntity>> sysUserRoleMap = sysUserRoleEntities.stream().collect(Collectors.groupingBy(SysUserRoleEntity::getRoleId));
        //需要创建策略的用户信息
        List<UserAndRoleRowPerssionDto> allRoleRowPerssionList = new ArrayList<>();
        for (Map.Entry<Long, List<SysUserRoleEntity>> entry : sysUserRoleMap.entrySet()) {
            if (CollectionUtil.isEmpty(entry.getValue())) {
                continue;
            }
//          通过角色查询到这个表和这个角色对应的行规则信息，如果没有，则这个角色对应其下所有的用户则不需要创建对应的行策略
//            List<RoleRowAndRoleDataPermissionDto> roleRowAndRoleDataPermissionDtos = roleRowDataPermissionConfigMapper.selectAllByRoleId(entry.getKey(), tableId);
            //修改此处策略，对所有用户生效
            List<RoleRowAndRoleDataPermissionDto> roleRowAndRoleDataPermissionDtos = tableInfoService.selectTableFileRuleIds(tableId);
            if (CollectionUtil.isNotEmpty(roleRowAndRoleDataPermissionDtos)) {
                UserAndRoleRowPerssionDto userAndRoleRowPerssionDto = new UserAndRoleRowPerssionDto();
                userAndRoleRowPerssionDto.setRoleId(entry.getKey());
                userAndRoleRowPerssionDto.setAllUserList(entry.getValue());
                userAndRoleRowPerssionDto.setAllPermissionList(roleRowAndRoleDataPermissionDtos);
                allRoleRowPerssionList.add(userAndRoleRowPerssionDto);
            }
        }
        //分别生成对应的用户对应的行规则策略，并且保存到ranger中去
        if (CollectionUtil.isEmpty(allRoleRowPerssionList)) {
            return;
        }
        // 行规则生成策略
        RangerPolicy rangerPolicy = pingRowLevelTOTable(tableId, allRoleRowPerssionList);

        if (null != rangerPolicy) {
            //直接新增
            RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicy);
            if (null != rangerPolicy1) {
                UserRangerPolicyRel userRangerPolicyRel1 = new UserRangerPolicyRel();
                userRangerPolicyRel1.setUserId(0L);
                userRangerPolicyRel1.setPolicyId(rangerPolicy1.getId());
                userRangerPolicyRel1.setCreateTime(DateTime.now());
                userRangerPolicyRel1.setTableId(tableId);
                userRangerPolicyRel1.setPolicyType(rangerPolicy1.getPolicyType());
                userRangerPolicyRelService.save(userRangerPolicyRel1);
            }
        }

    }

    @Override
    public void saveRangerPolicyMaskingByTableFieldId(Integer tableFieldId) {
        //查询到这个字段对应的表，得到这个表对应的所有的可以看到的用户，生成对应的策略（一个表一个字段一个策略）
        TableFieldMaskRules tableFieldMaskRules = tableFieldMaskRulesMapper.selectOne(new QueryWrapper<TableFieldMaskRules>().eq("table_field_id", tableFieldId));
        if (null == tableFieldMaskRules) {
            return;
        }
        //判断这个表对应的数据脱敏的策略是否开启
        TableInfo tableInfo = tableInfoMapper.selectById(tableFieldMaskRules.getTableId());
        if (null == tableInfo) {
            return;
        }
        //先删除这个策略，然后再重新生成
        List<UserRangerPolicyRel> userRangerPolicyRels = userRangerPolicyRelService.selectMaskingRulesData(tableFieldId);
        if (CollectionUtil.isNotEmpty(userRangerPolicyRels)) {
            for (UserRangerPolicyRel userRangerPolicyRel : userRangerPolicyRels) {
                try {
                    deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
                    userRangerPolicyRelService.deleteById(userRangerPolicyRel.getId());
                } catch (Exception e) {
                    log.error("删除数据脱敏的策略失败id:{},error:{}", userRangerPolicyRel.getPolicyId(), e.getMessage());
                }
            }
        }

        //是否关闭数据脱敏的功能
        if (Objects.equals(RangerMaskOpen.MASK_CLOSE.getId(), tableInfo.getTableMaskOpen())) {
            return;
        }
        //查询到这个表对应的所有的用户数据
        List<Long> userIds = tableInfoService.packUserIdByTableId(tableInfo.getId());
        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }
        //查询到这些用户对应的角色和表权限
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleService.queryUserAndRoleBy(userIds);
        if (CollectionUtil.isEmpty(sysUserRoleEntities)) {
            return;
        }
        //按照角色进行分组
        Map<Long, List<SysUserRoleEntity>> sysUserRoleMap = sysUserRoleEntities.stream().collect(Collectors.groupingBy(SysUserRoleEntity::getRoleId));
        //需要创建策略的用户信息
        List<UserAndRoleRowPerssionDto> allRoleRowPerssionList = new ArrayList<>();
        for (Map.Entry<Long, List<SysUserRoleEntity>> entry : sysUserRoleMap.entrySet()) {
            if (CollectionUtil.isEmpty(entry.getValue())) {
                continue;
            }
            //过滤白名单
            if (StringUtils.isNotEmpty(tableInfo.getTableMaskWhiteList())) {
                if (tableInfo.getTableMaskWhiteList().contains(entry.getKey().toString())) {
                    continue;
                }
            }

            //通过角色查询到这个表和这个角色对应的行规则信息，如果没有，则这个角色对应其下所有的用户则不需要创建对应的行策略
//            RoleRowAndRoleDataPermissionDto roleRowAndRoleDataPermissionDtos = roleRowDataPermissionConfigMapper.selectOneByRoleId(entry.getKey());
//            if(null == roleRowAndRoleDataPermissionDtos){
//                continue;
//            }
            UserAndRoleRowPerssionDto userAndRoleRowPerssionDto = new UserAndRoleRowPerssionDto();
            userAndRoleRowPerssionDto.setRoleId(entry.getKey());
            userAndRoleRowPerssionDto.setAllUserList(entry.getValue());
            allRoleRowPerssionList.add(userAndRoleRowPerssionDto);
        }
        if (CollectionUtil.isEmpty(allRoleRowPerssionList)) {
            return;
        }
        //生成对应的数据脱敏的策略
        RangerPolicy rangerPolicy = pingMaskRangerPolicy(tableInfo.getId(), tableFieldId, allRoleRowPerssionList);

        if (null != rangerPolicy) {
            //直接新增
            RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicy);
            if (null != rangerPolicy1) {
                UserRangerPolicyRel userRangerPolicyRel1 = new UserRangerPolicyRel();
                userRangerPolicyRel1.setUserId(0L);
                userRangerPolicyRel1.setPolicyId(rangerPolicy1.getId());
                userRangerPolicyRel1.setCreateTime(DateTime.now());
                userRangerPolicyRel1.setTableId(tableInfo.getId());
                userRangerPolicyRel1.setTableFieldId(tableFieldId);
                userRangerPolicyRel1.setPolicyType(rangerPolicy1.getPolicyType());
                userRangerPolicyRelService.save(userRangerPolicyRel1);
            }
        }

    }

    @Override
    public void saveRangerPolicyMasking(Integer tableId) {
        //先查询到这个表对应的配置的开关
        TableInfo tableInfo = tableInfoMapper.selectById(tableId);
        if(null == tableInfo){
            return;
        }
        //查询对应表的是否是开关状态
        //是否关闭数据脱敏的功能
        if (Objects.equals(RangerMaskOpen.MASK_CLOSE.getId(), tableInfo.getTableMaskOpen())) {
            //先删除这个策略，然后再重新生成
            List<UserRangerPolicyRel> userRangerPolicyRels = userRangerPolicyRelService.selectMaskingRulesDataByTableId(tableId);
            if (CollectionUtil.isNotEmpty(userRangerPolicyRels)) {
                for (UserRangerPolicyRel userRangerPolicyRel : userRangerPolicyRels) {
                    try {
                        deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
                        userRangerPolicyRelService.deleteById(userRangerPolicyRel.getId());
                    } catch (Exception e) {
                        log.error("删除数据脱敏的策略失败id:{},error:{}", userRangerPolicyRel.getPolicyId(), e.getMessage());
                    }
                }
            }
            return;
        }

        //重新生成旗下所有的脱敏规则
        List<TableFieldMaskRules> tableFieldMaskRules = tableFieldMaskRulesMapper.selectList(new QueryWrapper<TableFieldMaskRules>().eq("table_id", tableId));
        if(CollectionUtil.isNotEmpty(tableFieldMaskRules)){
            for (TableFieldMaskRules tableFieldMaskRule : tableFieldMaskRules) {
                saveRangerPolicyMaskingByTableFieldId(tableFieldMaskRule.getTableFieldId());
            }
        }

    }


    private RangerPolicy pingRowLevelTOTable(Integer tableId, List<UserAndRoleRowPerssionDto> allRoleRowPerssionList) {
        //生成对应表下所有的用户的行规则策略
        TableInfo tableInfo = tableInfoService.getById(tableId);
        if (null == tableInfo) {
            return null;
        }
        TableAndFieldDto tableAndFieldDto = new TableAndFieldDto();
        tableAndFieldDto.setDbName(tableInfo.getDbName());
        tableAndFieldDto.setTableName(tableInfo.getTableName());
        tableAndFieldDto.setTableInfoId(tableId);
        //封装表
        RangerPolicy rangerPolicy = pingRow_level_filteRangerPolicyTable(tableAndFieldDto);
        //封装用户和行权限
        if (CollectionUtil.isEmpty(allRoleRowPerssionList)) {
            return null;
        }
        for (UserAndRoleRowPerssionDto userAndRoleRowPerssionDto : allRoleRowPerssionList) {
            //根据行规则进行拼装对应的sql语句
//            if (null == userAndRoleRowPerssionDto || CollectionUtil.isEmpty(userAndRoleRowPerssionDto.getAllPermissionList()) || CollectionUtil.isEmpty(userAndRoleRowPerssionDto.getAllUserList())) {
//                continue;
//            }
            if (null == userAndRoleRowPerssionDto || CollectionUtil.isEmpty(userAndRoleRowPerssionDto.getAllUserList())) {
                continue;
            }
//            //验证库表等级----不满足库表权限，则返回true  相反则false
//            if (cheackTableAndDb(userAndRoleRowPerssionDto.getRoleId(), tableId)) {
//                continue;
//            }

            initRowPolicy(rangerPolicy, userAndRoleRowPerssionDto);
        }

        if (CollectionUtil.isEmpty(rangerPolicy.getRowFilterPolicyItems())) {
            return null;
        }
        return rangerPolicy;
    }

    private boolean cheackTableAndDb(Long roleId, Integer tableId) {
        //查询角色配置的库表权限
        if (null == roleId || null == tableId) {
            return true;
        }
        Set<Integer> dbIds = new HashSet<>();
        Set<Integer> tableLevelIds = new HashSet<>();
        tableInfoService.initDbAndTable(dbIds, tableLevelIds, roleId);
        //查询表对应的库表权限
        //获取到对应的全局默认配置
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
//        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());
        //拼装对应的表和表的字段信息
        TableAndFieldDto tableAndFieldDto = tableInfoMapper.getTableByIdToTable(tableId);
        if (null != tableAndFieldDto) {
            //表等级
            if (null == tableAndFieldDto.getTableLevelId()) {
                if (null != allPermissionConfig) {
                    tableAndFieldDto.setTableLevelId(allPermissionConfig.getDataPermissionId());
                }
            }
//                //表的字段
//                List<TableFieldDto> restTableFieldList = tableFieldInfoMapper.selectListByTableId(tableId);
//                if(CollectionUtil.isNotEmpty(restTableFieldList)){
//                    for (TableFieldDto tableFieldDto : restTableFieldList) {
//                        if(null == tableFieldDto.getColcumLevelId()){
//                            if(null != allPermissionConfigCulonm){
//                                tableFieldDto.setColcumLevelId(allPermissionConfigCulonm.getDataPermissionId());
//                            }
//                        }
//                    }
//                    tableAndFieldDto.setAllFieldList(restTableFieldList);
//                }
        }
        if (null == tableAndFieldDto) {
            return true;
        }
        if (CollectionUtil.isEmpty(dbIds) || CollectionUtil.isEmpty(tableLevelIds)) {
            return true;
        }
        //验证库等级
        if (dbIds.contains(tableAndFieldDto.getDbId())) {
            if (tableLevelIds.contains(tableAndFieldDto.getTableLevelId())) {
                return false;
            }
        }
        return true;
    }

    private void initRowPolicy(RangerPolicy rangerPolicy, UserAndRoleRowPerssionDto userAndRoleRowPerssionDto) {
        //将对应的行规则瓶装成一条sql语句
        List<SysUserRoleEntity> allUserList = userAndRoleRowPerssionDto.getAllUserList();
        List<RoleRowAndRoleDataPermissionDto> allPermissionList = userAndRoleRowPerssionDto.getAllPermissionList();
        //拼装sql
        StringBuffer sb = new StringBuffer();
        Set<Integer> tableRulesIds = new HashSet<>();
        Integer tableId = null;
        for (RoleRowAndRoleDataPermissionDto roleRowAndRoleDataPermissionDto : allPermissionList) {
            tableId = roleRowAndRoleDataPermissionDto.getTableId();
            tableRulesIds.add(roleRowAndRoleDataPermissionDto.getTableFieldRulesId());
        }
        if (CollectionUtil.isEmpty(tableRulesIds)) {
            return;
        }
//        List<RoleRowDataPermissionDto> roleRowDataPermissionDtos = tableFieldRulesMapper.selectAllRoleRowDataPermissionDtoListByTableFieldRulesIds(new ArrayList<>(tableRulesIds));
        List<RoleRowDataPermissionDto> roleRowDataPermissionDtos = tableFieldRulesMapper.selectRowData(tableId,new ArrayList<>(tableRulesIds));
        if (CollectionUtil.isEmpty(roleRowDataPermissionDtos)) {
            return;
        }
        String sql = pingSql(roleRowDataPermissionDtos);
        if (StringUtils.isEmpty(sql)) {
            return;
        }
        //生成对应的用户对应的行策略
        for (SysUserRoleEntity sysUserRoleEntity : allUserList) {
            initpingROW_LEVEL_FILTERRangerPolicy(rangerPolicy, sysUserRoleEntity.getUserId(), sql);
        }
    }

    private void insertRangerPolicyRow(Long rowParamId, TableFieldRules tableFieldRules, String sql) {
        if (StringUtils.isEmpty(sql)) {
            return;
        }
        //当前角色下的用户信息
        RoleDataPermissionConfig roleDataPermissionConfig = roleDataPermissionConfigMapper.selectById(rowParamId);
        if (null == roleDataPermissionConfig) {
            return;
        }
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleService.list(new QueryWrapper<SysUserRoleEntity>().eq("role_id", roleDataPermissionConfig.getRoleId()));
        if (CollectionUtil.isEmpty(sysUserRoleEntities)) {
            return;
        }
        //挑选出可以看到这个表的用户信息
        List<Long> userIds = pickToTableInfoUser(sysUserRoleEntities, tableFieldRules.getTableId());
        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }

        //根据同一角色下的用户对应的权限生成策略，并且生成对应的策略信息
        //初始化权限
        Set<Integer> allDbIds = new HashSet<>();
        Set<Integer> alltableIds = new HashSet<>();
        Set<Integer> allConlumIds = new HashSet<>();
        tableInfoService.initDbAndTableAndColumIds(allDbIds, alltableIds, allConlumIds, new HashSet<>(), roleDataPermissionConfig.getRoleId());


        //获取到对应的全局默认配置
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());
        //拼装对应的表和表的字段信息
        TableAndFieldDto tableAndFieldDto = tableInfoMapper.getTableByIdToTable(tableFieldRules.getTableId());
        if (null != tableAndFieldDto) {
            //表等级
            if (null == tableAndFieldDto.getTableLevelId()) {
                if (null != allPermissionConfig) {
                    tableAndFieldDto.setTableLevelId(allPermissionConfig.getDataPermissionId());
                }
            }
            //表的字段
            List<TableFieldDto> restTableFieldList = tableFieldInfoMapper.selectListByTableId(tableFieldRules.getTableId());
            if (CollectionUtil.isNotEmpty(restTableFieldList)) {
                for (TableFieldDto tableFieldDto : restTableFieldList) {
                    if (null == tableFieldDto.getColcumLevelId()) {
                        if (null != allPermissionConfigCulonm) {
                            tableFieldDto.setColcumLevelId(allPermissionConfigCulonm.getDataPermissionId());
                        }
                    }
                }
                tableAndFieldDto.setAllFieldList(restTableFieldList);
            }
        }

        //生成对应的策略---这个行策略一个表只能生成一个【ranger的bug】
        //判断库对应的等级权限
        if (allDbIds.contains(tableAndFieldDto.getDbId())) {
            //判断表对应的等级权限
            if (alltableIds.contains(tableAndFieldDto.getTableLevelId())) {
                //判断列对应的等级权限
                if (CollectionUtil.isEmpty(tableAndFieldDto.getAllFieldList())) {
                    return;
                }
                List<String> allSelect = listAllColumn(tableAndFieldDto.getAllFieldList(), allConlumIds);
                if (CollectionUtil.isEmpty(allSelect)) {
                    return;
                }
                //拼装对应的表的权限resourse---根据当前用户
                RangerPolicy rangerPolicy = pingRow_level_filteRangerPolicyTable(tableAndFieldDto);
                for (Long userId : userIds) {
                    initpingROW_LEVEL_FILTERRangerPolicy(rangerPolicy, userId, sql);
                }

                //先查询这个表是否存在这个表对应的行权限。如果有，则先删除，在新增。如果没有则直接新增

            }
        }

//        if(CollectionUtil.isNotEmpty(rangerPolicies)){
//            for (int i = 0; i <= rangerPolicies.size() / 10; i++) {
//                List<RangerPolicyDto> collect = rangerPolicies.stream().skip(i * 10).limit(10).collect(Collectors.toList());
//                myExecutor.execute(new Runnable() {
//                    @Override
//                    public void run() {
//                        List<UserRangerPolicyRel> userRangerPolicyRels = new ArrayList<>();
//                        for (RangerPolicyDto rangerPolicy : collect) {
//                            List<RangerPolicy> rangerPolicies1 = rangerPolicy.getRangerPolicies();
//                            if(CollectionUtil.isNotEmpty(rangerPolicies1)){
//                                RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicies1.get(0));
//                                if (null != rangerPolicy1) {
//                                    UserRangerPolicyRel userRangerPolicyRel = new UserRangerPolicyRel();
//                                    userRangerPolicyRel.setUserId(rangerPolicy.getUserId());
//                                    userRangerPolicyRel.setPolicyId(rangerPolicy1.getId());
//                                    userRangerPolicyRel.setCreateTime(DateTime.now());
//                                    userRangerPolicyRel.setTableId(tableFieldRules.getTableId());
//                                    userRangerPolicyRel.setPolicyType(rangerPolicies1.get(0).getPolicyType());
//                                    userRangerPolicyRels.add(userRangerPolicyRel);
//                                }
//                            }
//                        }
//                        //批量保存关联信息
//                        if (CollectionUtil.isNotEmpty(userRangerPolicyRels)) {
//                            userRangerPolicyRelService.saveBatch(userRangerPolicyRels);
//                        }
//                    }
//                });
//            }
//        }

    }

    private List<Long> pickToTableInfoUser(List<SysUserRoleEntity> sysUserRoleEntities, Integer tableId) {
        if (CollectionUtil.isEmpty(sysUserRoleEntities)) {
            return null;
        }

        List<Long> olderUserIds = new ArrayList<>();
        for (SysUserRoleEntity sysUserRoleEntity : sysUserRoleEntities) {
            olderUserIds.add(sysUserRoleEntity.getUserId());
        }

        //查询到所有的这个用户对应的所有的表
        Set<Long> allUserIds = getAllUserIdsByProc(tableId);
        if (CollectionUtil.isEmpty(allUserIds)) {
            return null;
        }
        Set<Long> resUserIds = new HashSet<>();
        allUserIds.forEach(id -> {
            if (olderUserIds.contains(id)) {
                resUserIds.add(id);
            }
        });
        return new ArrayList<>(resUserIds);
    }

    private Set<Long> getAllUserIdsByProc(Integer tableId) {
        if (null == tableId) {
            return null;
        }
        //先查询自己的表是否有不存在的。
        TableInfo tableInfo1 = tableInfoMapper.selectById(tableId);
        if (null == tableInfo1) {
            return null;
        }
        Set<Long> allUserIds = new HashSet<>();
        allUserIds.add(Long.valueOf(tableInfo1.getCreatePer()));
        //再查询加入的项目是否，关联度表是否存在
        List<Long> userIds = procUserMapper.selectByTableId(tableId);
        if (CollectionUtil.isNotEmpty(userIds)) {
            allUserIds.addAll(userIds);
        }
        return allUserIds;
    }


    @Override
    public void genteratorRangerPolicy(Integer tableId) {

        //组装对应的用户信息表信息
        //重新生成这个表的行规则和脱敏规则
        List<Integer> tableIds = new ArrayList<>();
        tableIds.add(tableId);
        //initAllHaveMaskRulesTablePolicyByTableIds(tableIds);
        initAllHaveRowRulesTablePolicyByTableIds(tableIds);

    }

    @Override
    public void saveAllPolicy(Integer roleId) {
        //根据角色id查询到所有的用户
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleService.list(new QueryWrapper<SysUserRoleEntity>().eq("role_id", roleId));
        if (CollectionUtil.isEmpty(sysUserRoleEntities)) {
            return;
        }

        //生成对应的所有的用户的策略
        Set<Long> userIds = new HashSet<>();
        for (SysUserRoleEntity sysUserRoleEntity : sysUserRoleEntities) {
            userIds.add(sysUserRoleEntity.getUserId());
        }
        if(CollectionUtil.isEmpty(userIds)){
            return;
        }
        List<Long> newUserIds = new ArrayList<>(userIds);
        myExecutor.execute(new Runnable() {
            @Override
            public void run() {
                //根据所有的用户的策略
                genteratorRangerPolicy(newUserIds);
            }
        });
    }

    public void deleteRangerPolicyByRoleRowDataPermissionConfigId(Integer id) {
        //查询到对应的配置信息
        RoleRowDataPermissionConfig roleRowDataPermissionConfig = roleRowDataPermissionConfigMapper.selectById(id);
        if (null == roleRowDataPermissionConfig) {
            return;
        }
        //删除这个角色下所有的用户，对应的这个表的策略
        //查询到所有的这个用户对应的所有的表
        RoleDataPermissionConfig roleDataPermissionConfig = roleDataPermissionConfigMapper.selectById(roleRowDataPermissionConfig.getRoleDataPermissionId());
        if (null == roleDataPermissionConfig) {
            return;
        }
        List<SysUserRoleEntity> sysUserRoleEntities = sysUserRoleService.list(new QueryWrapper<SysUserRoleEntity>().eq("role_id", roleDataPermissionConfig.getRoleId()));
        if (CollectionUtil.isEmpty(sysUserRoleEntities)) {
            return;
        }
        List<Long> allUserIds = pickToTableInfoUser(sysUserRoleEntities, roleRowDataPermissionConfig.getTableId());
        if (CollectionUtil.isEmpty(allUserIds)) {
            return;
        }
        //并且删除对应的权限策略
        try {
            List<UserRangerPolicyRel> userRangerPolicyRels = userRangerPolicyRelService.selectAllListByUserIdsAndTableId(roleRowDataPermissionConfig.getTableId(), allUserIds);
            if (CollectionUtil.isNotEmpty(userRangerPolicyRels)) {
                for (UserRangerPolicyRel userRangerPolicyRel : userRangerPolicyRels) {
                    try {
                        deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
                    } catch (Exception e) {
                        log.error("删除策略异常：error:{}", e.getMessage());
                    }
                    userRangerPolicyRelService.removeById(userRangerPolicyRel.getId());
                }
            }
        } catch (Exception e) {
            log.error("删除策略失败！error:{}", e.getMessage());
        }
    }

    @Override
    public void deleteRangerPolicy(ProcUser procUser) {
        if (null == procUser) {
            return;
        }
        //查询到这个用户的信息
        List<ProcTable> procTables = procTableMapper.selectList(new QueryWrapper<ProcTable>().eq("proc_id", procUser.getProcId()));
        if (CollectionUtil.isEmpty(procTables)) {
            return;
        }
        List<Integer> tableIds = new ArrayList<>();
        for (ProcTable procTable : procTables) {
            tableIds.add(procTable.getTableId());
        }
        List<UserRangerPolicyRel> restList = userRangerPolicyRelService.selectAllListByProcIdAndList(procUser.getUserId(), tableIds);
        if (CollectionUtil.isEmpty(restList)) {
            return;
        }
        for (UserRangerPolicyRel userRangerPolicyRel : restList) {
            userRangerPolicyRelService.removeById(userRangerPolicyRel.getId());
            deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
        }
    }

    @Override
    public void saveRangerPolicyByTableId(Integer tableId, Long userId) {
        //新增表的时候，需要新增这个表对应的策略
        //查询到当前用户对应的角色配置的权限
        SysUserDto sysUserDto = sysUserRoleService.queryUserInfoByUserId(userId,ShiroUtils.getTenantId());
        if (null == sysUserDto) {
            return;
        }
        if (CollectionUtil.isEmpty(sysUserDto.getRoleIdList())) {
            return;
        }
        //初始化权限
        Set<Integer> allDbIds = new HashSet<>();
        Set<Integer> alltableIds = new HashSet<>();
        Set<Integer> allConlumIds = new HashSet<>();
        tableInfoService.initDbAndTableAndColumIds(allDbIds, alltableIds, allConlumIds, new HashSet<>(), sysUserDto.getRoleIdList().get(0));

        if (CollectionUtil.isEmpty(allDbIds) || CollectionUtil.isEmpty(alltableIds) || CollectionUtil.isEmpty(allConlumIds)) {
            return;
        }

        //查询表对应的dbId
        TableAndFieldDto tableByIdToTable = tableInfoMapper.getTableByIdToTable(tableId);
        if (null == tableByIdToTable) {
            return;
        }

        //查询默认的权限配置数据
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());

        //生成对应的策略
        //判断库对应的等级权限
        if (allDbIds.contains(tableByIdToTable.getDbId())) {
            //判断表对应的等级权限
            if (alltableIds.contains(allPermissionConfig.getDataPermissionId())) {
                //判断列对应的等级权限
                if (allConlumIds.contains(allPermissionConfigCulonm.getDataPermissionId())) {
                    List<TableFieldDto> tableFieldDtos = tableFieldInfoMapper.selectListByTableId(tableId);
                    if (CollectionUtil.isEmpty(tableFieldDtos)) {
                        return;
                    }
                    List<String> tableList = new ArrayList<>();
//                    for (TableFieldDto tableFieldDto : tableFieldDtos) {
//                        tableList.add(tableFieldDto.getFieldName());
//                    }
                    //直接*号查看所有的字段
                    tableList.add("*");
                    RangerPolicy rangerPolicy = pingACCESSRangerPolicy(userId, tableList, tableId, tableByIdToTable.getTableName(), tableByIdToTable.getDbName());
                    RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicy);
                    if (null != rangerPolicy1) {
                        UserRangerPolicyRel userRangerPolicyRel = new UserRangerPolicyRel();
                        userRangerPolicyRel.setUserId(userId);
                        userRangerPolicyRel.setPolicyId(rangerPolicy1.getId());
                        userRangerPolicyRel.setCreateTime(DateTime.now());
                        userRangerPolicyRel.setTableId(tableId);
                        userRangerPolicyRel.setPolicyType(rangerPolicy.getPolicyType());
                        userRangerPolicyRelService.save(userRangerPolicyRel);

                    }
                }
            }
        }

    }

    @Override
    public void deleteRangerPloicyByTableId(Integer tableId) {
        //删除表的时候删除表对应的所有的策略
        if (null != tableId) {
            List<UserRangerPolicyRel> userRangerPolicyRels = userRangerPolicyRelService.list(new QueryWrapper<UserRangerPolicyRel>().eq("table_id", tableId));
            if(CollectionUtil.isNotEmpty(userRangerPolicyRels)){
                for (UserRangerPolicyRel userRangerPolicyRel : userRangerPolicyRels) {
                    userRangerPolicyRelService.deleteById(userRangerPolicyRel.getId());
                    try {
                        deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
                    } catch (Exception e) {
                       log.warn("策略删除异常,策略不存在{}",userRangerPolicyRel.getPolicyId());
                    }
                }
            }
        }
    }

    @Override
    public void updateRangerPolicyByTableId(Integer tableId) {

        //修改表的时候修改这个表对应的所有的策略
        List<UserAndTableDto> userAndTableDtos = tableInfoService.packUserAndTableData(tableId);
        if (CollectionUtil.isEmpty(userAndTableDtos)) {
            return;
        }
        List<RangerPolicyDto> rangerPolicyDtos = listAllUserAndTablesPression(3, userAndTableDtos);
        if (CollectionUtil.isEmpty(rangerPolicyDtos)) {
            return;
        }

        //直接删除对应表的所有的策略，然后重新生成策略
        //查询到对应的关联关系
        List<UserRangerPolicyRel> listAllUserRangerPolicyRels = userRangerPolicyRelService.list(new QueryWrapper<UserRangerPolicyRel>()
                .eq("table_id", tableId)
                //行规则特殊，自己创建
                .eq("policy_type", RangerPolicyType.ACCESS.getId()));
        if (CollectionUtil.isNotEmpty(listAllUserRangerPolicyRels)) {
            for (UserRangerPolicyRel listAllUserRangerPolicyRel : listAllUserRangerPolicyRels) {
                try {
                    deleteRangerPolicy(listAllUserRangerPolicyRel.getPolicyId());
                } catch (Exception e) {
                    log.error("删除ranger-策略异常！{}", e.getMessage());
                }
                userRangerPolicyRelService.removeById(listAllUserRangerPolicyRel.getId());
            }
        }

        //生成对应的策略，并且保存关联信息
        for (RangerPolicyDto rangerPolicyDto : rangerPolicyDtos) {
            if (null != rangerPolicyDto && CollectionUtil.isNotEmpty(rangerPolicyDto.getRangerPolicies())) {
                List<RangerPolicy> rangerPolicies = rangerPolicyDto.getRangerPolicies();
                for (int i = 0; i <= rangerPolicies.size() / 10; i++) {
                    List<RangerPolicy> collect = rangerPolicies.stream().skip(i * 10).limit(10).collect(Collectors.toList());
                    myExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            List<UserRangerPolicyRel> userRangerPolicyRels = new ArrayList<>();
                            for (RangerPolicy rangerPolicy : collect) {
                                RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicy);
                                if (null != rangerPolicy1) {
                                    UserRangerPolicyRel userRangerPolicyRel = new UserRangerPolicyRel();
                                    userRangerPolicyRel.setUserId(rangerPolicyDto.getUserId());
                                    userRangerPolicyRel.setPolicyId(rangerPolicy1.getId());
                                    userRangerPolicyRel.setCreateTime(DateTime.now());
                                    userRangerPolicyRel.setPolicyType(rangerPolicy1.getPolicyType());
                                    String[] split = rangerPolicy.getName().split("_");
                                    if (null != split && split.length >= 3) {
                                        userRangerPolicyRel.setTableId(Integer.valueOf(split[2]));
                                    }
                                    userRangerPolicyRels.add(userRangerPolicyRel);
                                }
                            }
                            //批量保存关联信息
                            if (CollectionUtil.isNotEmpty(userRangerPolicyRels)) {
                                userRangerPolicyRelService.saveBatch(userRangerPolicyRels);
                            }
                        }
                    });
                }
            }
        }

    }

    @Override
    public void initPolicyDataList() {

        //查询所有的用户信息
        List<SysUserEntity> list = sysUserService.list(new QueryWrapper<SysUserEntity>());
        if (CollectionUtil.isNotEmpty(list)) {
            List<Long> userIds = new ArrayList<>();
            for (SysUserEntity sysUserEntity : list) {
                userIds.add(sysUserEntity.getUserId());
            }
            //再新增用户对应的表的策略---包括先删除，再新增
            genteratorRangerPolicy(userIds);
        }
    }


    public RangerPolicy deleteAndAddRangerPolicy() {

        //先删除对应的策略，再新增对应的策略
        UserRangerPolicyRel userRangerPolicyRel = userRangerPolicyRelService.getOne(new QueryWrapper<UserRangerPolicyRel>()
                .eq("table_id", 0)
                .eq("user_id", 0)
                .eq("policy_type", RangerPolicyType.EXPLORE_TABLE.getId()));

        if (null != userRangerPolicyRel) {
            deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
            userRangerPolicyRelService.deleteById(userRangerPolicyRel.getId());
        }

        RangerPolicy rangerPolicy = pingExplorePolicy();

        RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicy);

        if (null != rangerPolicy1) {
            UserRangerPolicyRel userRangerPolicyRel1 = new UserRangerPolicyRel();
            userRangerPolicyRel1.setUserId(0L);
            userRangerPolicyRel1.setPolicyId(rangerPolicy1.getId());
            userRangerPolicyRel1.setCreateTime(DateTime.now());
            userRangerPolicyRel1.setTableId(0);
            userRangerPolicyRel1.setPolicyType(rangerPolicy1.getPolicyType());
            userRangerPolicyRelService.save(userRangerPolicyRel1);
        }

        return rangerPolicy1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteRangerPolicyMasking(Integer tableFieldId) {
        //查询到对应的策略
        List<UserRangerPolicyRel> list = userRangerPolicyRelService.selectMaskingRulesData(tableFieldId);
        if (CollectionUtil.isNotEmpty(list)) {
            for (UserRangerPolicyRel userRangerPolicyRel : list) {
                userRangerPolicyRelService.deleteById(userRangerPolicyRel.getId());
                deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
            }
        }
    }

    @Override
    public void deleteRangerPloicyByProcIdAndTableId(Integer proc_id, Integer table_id) {
        //删除这个项目中其它组员对应这个表的access权限策略
        ProcInfo procInfo = procInfoMapper.selectById(proc_id);
        if (null == procInfo) {
            return;
        }
        TableInfo tableInfo = tableInfoMapper.selectById(table_id);
        if (null == tableInfo) {
            return;
        }
        List<ProcUser> procUsers = procUserMapper.selectList(new QueryWrapper<ProcUser>()
                .eq("proc_id", proc_id)
                .ne("user_id", tableInfo.getCreatePer()));
        if (CollectionUtil.isEmpty(procUsers)) {
            return;
        }
        List<Long> userIds = new ArrayList<>();
        procUsers.forEach(procUser -> {
            userIds.add(Long.valueOf(procUser.getUserId()));
        });
        List<UserRangerPolicyRel> userRangerPolicyRels = userRangerPolicyRelService.selectAllListByUserIdsAndTableId(table_id, userIds);
        if (CollectionUtil.isEmpty(userRangerPolicyRels)) {
            return;
        }
        for (UserRangerPolicyRel userRangerPolicyRel : userRangerPolicyRels) {
            userRangerPolicyRelService.deleteById(userRangerPolicyRel.getId());
            deleteRangerPolicy(userRangerPolicyRel.getPolicyId());
        }
    }

    @Override
    public void saveRangerPolicyByProcIdAndTableId(Integer procId, Integer tableId) {
        //增加这个项目组里其它的成员对这个表的access权限策略
        ProcInfo procInfo = procInfoMapper.selectById(procId);
        if (null == procInfo) {
            return;
        }
        TableInfo tableInfo = tableInfoMapper.selectById(tableId);
        if (null == tableInfo) {
            return;
        }
        //如果关联的是谁的，谁针对这个表就不用创建了。因为新建表的时候已经新增了的
        List<ProcUser> procUsers = procUserMapper.selectList(new QueryWrapper<ProcUser>()
                .eq("proc_id", procId)
                .ne("user_id", tableInfo.getCreatePer()));
        if (CollectionUtil.isEmpty(procUsers)) {
            return;
        }
        List<Long> userIds = new ArrayList<>();
        procUsers.forEach(procUser -> {
            userIds.add(Long.valueOf(procUser.getUserId()));
        });

        //重新生这些用户的这些表的策略--Access策略
        for (Long userId : userIds) {
            //这个地方需要修改
            saveRangerPolicyByOlerTable(tableId, userId);
        }

        //重新生成行规则策略
        saveRangerPolicy(tableId);

        //重新生成脱敏规则策略
        saveRangerPolicyMasking(tableId);
    }

    private void saveRangerPolicyByOlerTable(Integer tableId, Long userId) {

        //查询到当前用户对应的角色配置的权限
        SysUserDto sysUserDto = sysUserRoleService.queryUserInfoByUserId(userId,ShiroUtils.getTenantId());
        if (null == sysUserDto) {
            return;
        }
        if (CollectionUtil.isEmpty(sysUserDto.getRoleIdList())) {
            return;
        }
        //初始化权限
        Set<Integer> allDbIds = new HashSet<>();
        Set<Integer> alltableIds = new HashSet<>();
        Set<Integer> allConlumIds = new HashSet<>();
        tableInfoService.initDbAndTableAndColumIds(allDbIds, alltableIds, allConlumIds, new HashSet<>(), sysUserDto.getRoleIdList().get(0));

        if (CollectionUtil.isEmpty(allDbIds) || CollectionUtil.isEmpty(alltableIds) || CollectionUtil.isEmpty(allConlumIds)) {
            return;
        }

        //查询表对应的dbId
        TableAndFieldDto tableByIdToTable = tableInfoMapper.getTableByIdToTable(tableId);
        if (null == tableByIdToTable) {
            return;
        }

        //查询默认的权限配置数据
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());

        Integer tableLevelId = allPermissionConfig.getDataPermissionId();
        if (null != tableByIdToTable.getTableLevelId() && 0 != tableByIdToTable.getTableLevelId()) {
            tableLevelId = tableByIdToTable.getTableLevelId();
        }

        //生成对应的策略
        //判断库对应的等级权限
        if (allDbIds.contains(tableByIdToTable.getDbId())) {
            //判断表对应的等级权限
            if (alltableIds.contains(tableLevelId)) {
                //判断列对应的等级权限
                List<TableFieldDto> tableFieldDtos = tableFieldInfoMapper.selectListByTableId(tableId);
                if (CollectionUtil.isEmpty(tableFieldDtos)) {
                    return;
                }
                List<String> allNeedHiveTableInfos = new ArrayList<>();
                for (TableFieldDto tableFieldDto : tableFieldDtos) {
                    Integer colcumLevelId = allPermissionConfigCulonm.getDataPermissionId();
                    if (null != tableFieldDto.getColcumLevelId() && 0 != tableFieldDto.getColcumLevelId()) {
                        colcumLevelId = tableFieldDto.getColcumLevelId();
                    }
                    if (allConlumIds.contains(colcumLevelId)) {
                        allNeedHiveTableInfos.add(tableFieldDto.getFieldName());
                    }
                }
                if (CollectionUtil.isEmpty(allNeedHiveTableInfos)) {
                    return;
                }
                if (allNeedHiveTableInfos.size() == tableFieldDtos.size()) {
                    allNeedHiveTableInfos.clear();
                    allNeedHiveTableInfos.add("*");
                }
                RangerPolicy rangerPolicy = pingACCESSRangerPolicy(userId, allNeedHiveTableInfos, tableId, tableByIdToTable.getTableName(), tableByIdToTable.getDbName());
                RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicy);
                if (null != rangerPolicy1) {
                    UserRangerPolicyRel userRangerPolicyRel = new UserRangerPolicyRel();
                    userRangerPolicyRel.setUserId(userId);
                    userRangerPolicyRel.setPolicyId(rangerPolicy1.getId());
                    userRangerPolicyRel.setCreateTime(DateTime.now());
                    userRangerPolicyRel.setTableId(tableId);
                    userRangerPolicyRel.setPolicyType(rangerPolicy.getPolicyType());
                    userRangerPolicyRelService.save(userRangerPolicyRel);

                }

            }
        }
    }

    @Override
    public void saveRangerPolicyROWFirlterByProcId(Integer procId) {
        //查询到这个工程里面关联的表
        List<ProcTable> procTables = procTableMapper.selectList(new QueryWrapper<ProcTable>().eq("proc_id", procId));
        if (CollectionUtil.isEmpty(procTables)) {
            return;
        }
        for (ProcTable procTable : procTables) {
            //重新生成行规则策略
            saveRangerPolicy(procTable.getTableId());
        }
    }

    @Override
    public void deleteAllUserRangerRel() {
        userRangerPolicyRelService.remove(new QueryWrapper<UserRangerPolicyRel>());
    }


    private RangerPolicy pingExplorePolicy() {
        RangerPolicy rangerPolicy = new RangerPolicy();
        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();

        //字段
        RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
        rangerPolicyResource.getValues().add("*");
        rangerPolicyResource.setIsExcludes(false);
        rangerPolicyResource.setIsRecursive(false);

        //表
        RangerPolicyResource rangerPolicyResourceTable = new RangerPolicyResource();
        rangerPolicyResourceTable.getValues().add("*");
        rangerPolicyResourceTable.setIsExcludes(false);
        rangerPolicyResourceTable.setIsRecursive(false);

        //库
        RangerPolicyResource rangerPolicyResourceDb = new RangerPolicyResource();
        rangerPolicyResourceDb.getValues().add("explore");
        rangerPolicyResourceDb.setIsExcludes(false);
        rangerPolicyResourceDb.setIsRecursive(false);
        //对应的库的资源
        resourceMap.put(RangerDBType.COLUMN.getName(), rangerPolicyResource);
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResourceTable);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResourceDb);
        rangerPolicy.setResources(resourceMap);

        //通用配置
        RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
        rangerPolicyItem.getAccesses().addAll(listRangerPolicyItemAccess());
        rangerPolicyItem.getGroups().add(dataDevelopmentConfig.getExploreGroup());
        rangerPolicyItem.getUsers().add("{USER}");
        rangerPolicy.getPolicyItems().add(rangerPolicyItem);


//        //查询所有用户
//        List<SysUserEntity> list = sysUserService.list();
//        if (CollectionUtil.isEmpty(list)) {
//            return null;
//        }
//
//        for (int i = 0; i <= list.size() / 10; i++) {
//            List<SysUserEntity> collect = list.stream().skip(i * 10).limit(10).collect(Collectors.toList());
//            List<String> userIds = new ArrayList<>();
//            for (SysUserEntity sysUserEntity : collect) {
//                userIds.add(dataDevelopmentConfig.getUserPreffix() + sysUserEntity.getUserId());
//            }
//            RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
//            rangerPolicyItem.getAccesses().add(new RangerPolicyItemAccess(RangerPolicyItemAccessType.ALL.getName(), true));
//
//            rangerPolicyItem.getUsers().addAll(userIds);
//            rangerPolicy.getPolicyItems().add(rangerPolicyItem);
//        }


        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName("common_explore_policy");
        rangerPolicy.setPolicyType(RangerPolicyType.ACCESS.getId());
        rangerPolicy.setAuditEnabled(true);

        return rangerPolicy;
    }


    private List<RangerPolicyItemAccess> listRangerPolicyItemAccess() {
        List<RangerPolicyItemAccess> restList = new ArrayList<>();
        for (int i = 0; i <= 9; i++) {
            RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
            rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.ObjOf(i).getName());
            rangerPolicyItemAccess.setIsAllowed(true);
            restList.add(rangerPolicyItemAccess);
        }
        return restList;
    }

    /***
     * 生成对应的表的策略，暂定一个表至少一个策略
     * @param allDbIds                    所有的库等级
     * @param alltableIds                 所有的表等级
     * @param allConlumIds                所有的列等级
     * @param rangerPolicyDto
     * @param userAndTableDto
     */
    private void initPressionAndRangerPolicyDto(Integer type,
                                                Set<Integer> allDbIds,
                                                Set<Integer> alltableIds,
                                                Set<Integer> allConlumIds,
                                                RangerPolicyDto rangerPolicyDto,
                                                UserAndTableDto userAndTableDto) {
        List<RangerPolicy> rangerPolicies = new ArrayList<>();
        if (!CollectionUtil.isEmpty(allDbIds) &&
                !CollectionUtil.isEmpty(alltableIds) &&
                !CollectionUtil.isEmpty(allConlumIds)) {
            if (CollectionUtil.isNotEmpty(userAndTableDto.getAllTableAndFieldDtoList())) {
                for (TableAndFieldDto tableAndFieldDto : userAndTableDto.getAllTableAndFieldDtoList()) {
                    //判断库对应的等级权限
                    if (allDbIds.contains(tableAndFieldDto.getDbId())) {
                        //判断表对应的等级权限
                        if (alltableIds.contains(tableAndFieldDto.getTableLevelId())) {
                            //判断列对应的等级权限
                            if (CollectionUtil.isEmpty(tableAndFieldDto.getAllFieldList())) {
                                continue;
                            }
                            List<String> allSelect = listAllColumn(tableAndFieldDto.getAllFieldList(), allConlumIds);
                            if (CollectionUtil.isEmpty(allSelect)) {
                                continue;
                            }
                            //拼装对应的表的权限resourse
                            if (RangerPolicyType.ACCESS.getId() == type || 3 == type) {
                                RangerPolicy rangerPolicy = pingACCESSRangerPolicy(allSelect, tableAndFieldDto, userAndTableDto);
                                rangerPolicies.add(rangerPolicy);
                            }
                        }
                    }
                }
            }
        }
        //初始化一个未知的数据库ABCDDCBA
        if (CollectionUtil.isEmpty(rangerPolicies)) {
            packExcludeAllData(rangerPolicyDto, userAndTableDto.getUserId());
        } else {
            rangerPolicyDto.setRangerPolicies(rangerPolicies);
        }
    }

    private RangerPolicy pingRow_level_filteRangerPolicyTable(TableAndFieldDto tableAndFieldDto) {
        RangerPolicy rangerPolicy = new RangerPolicy();
        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();
        //表
        // 行规则只能有一个
        RangerPolicyResource rangerPolicyResourceTable = new RangerPolicyResource();
        rangerPolicyResourceTable.getValues().add(tableAndFieldDto.getTableName());
        rangerPolicyResourceTable.setIsExcludes(false);
        rangerPolicyResourceTable.setIsRecursive(false);

        //库
        RangerPolicyResource rangerPolicyResourceDb = new RangerPolicyResource();
        rangerPolicyResourceDb.getValues().add(tableAndFieldDto.getDbName());
        rangerPolicyResourceDb.setIsExcludes(false);
        rangerPolicyResourceDb.setIsRecursive(false);

        //对应的库的资源
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResourceTable);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResourceDb);
        rangerPolicy.setResources(resourceMap);


        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName(dataDevelopmentConfig.getTablePolicyPreffix() + "0_" + tableAndFieldDto.getTableInfoId() + "_" + RangerPolicyType.ROW_LEVEL_FILTER.getId());
        rangerPolicy.setPolicyType(RangerPolicyType.ROW_LEVEL_FILTER.getId());
        rangerPolicy.setAuditEnabled(true);

        return rangerPolicy;

    }

    private void initpingROW_LEVEL_FILTERRangerPolicy(RangerPolicy rangerPolicy, Long userId, String sql) {

        //拼接行规则sql语句
        if (StringUtils.isEmpty(sql)) {
            return;
        }

        //通用配置
        RangerRowFilterPolicyItem rangerRowFilterPolicyItem = new RangerRowFilterPolicyItem();
        rangerRowFilterPolicyItem.getAccesses().add(new RangerPolicyItemAccess(RangerPolicyItemAccessType.SELECT.getName(), true));
        rangerRowFilterPolicyItem.getUsers().addAll(Arrays.asList(dataDevelopmentConfig.getUserPreffix() + userId));
        RangerRowFilterInfo rangerRowFilterInfo = new RangerRowFilterInfo();
        rangerRowFilterInfo.setFilterExpr(sql);
        rangerRowFilterPolicyItem.setRowFilterInfo(rangerRowFilterInfo);
        rangerPolicy.getRowFilterPolicyItems().add(rangerRowFilterPolicyItem);

    }

    private RangerPolicy pingROW_LEVEL_FILTERRangerPolicy(List<RoleRowDataPermissionDto> roleRowDataPermissionDtos, TableAndFieldDto tableAndFieldDto, UserAndTableDto userAndTableDto) {

        RangerPolicy rangerPolicy = new RangerPolicy();
        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();
        //表
        //添加一个唯一标识的表，防止对用户针对同一个表的时候的权限问题【bug】---ranger的bug【不同用户针对一个表设置相同的策略会报错】
        RangerPolicyResource rangerPolicyResourceTable = new RangerPolicyResource();
        rangerPolicyResourceTable.getValues().add(tableAndFieldDto.getTableName());
        rangerPolicyResourceTable.getValues().add(UUID.randomUUID().toString());
        rangerPolicyResourceTable.setIsExcludes(false);
        rangerPolicyResourceTable.setIsRecursive(false);

        //库
        RangerPolicyResource rangerPolicyResourceDb = new RangerPolicyResource();
        rangerPolicyResourceDb.getValues().add(tableAndFieldDto.getDbName());
        rangerPolicyResourceDb.setIsExcludes(false);
        rangerPolicyResourceDb.setIsRecursive(false);
        //对应的库的资源
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResourceTable);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResourceDb);
        rangerPolicy.setResources(resourceMap);

        //拼接行规则sql语句
        String sql = pingSql(roleRowDataPermissionDtos);
        if (StringUtils.isEmpty(sql)) {
            return null;
        }

        //通用配置
        RangerRowFilterPolicyItem rangerRowFilterPolicyItem = new RangerRowFilterPolicyItem();
        rangerRowFilterPolicyItem.getAccesses().add(new RangerPolicyItemAccess(RangerPolicyItemAccessType.SELECT.getName(), true));
        rangerRowFilterPolicyItem.getUsers().addAll(Arrays.asList(dataDevelopmentConfig.getUserPreffix() + userAndTableDto.getUserId()));
        RangerRowFilterInfo rangerRowFilterInfo = new RangerRowFilterInfo();
        rangerRowFilterInfo.setFilterExpr(sql);
        rangerRowFilterPolicyItem.setRowFilterInfo(rangerRowFilterInfo);
        rangerPolicy.getRowFilterPolicyItems().add(rangerRowFilterPolicyItem);

        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName(dataDevelopmentConfig.getUserPolicyPreffix() + userAndTableDto.getUserId() + "_" + tableAndFieldDto.getTableInfoId() + "_" + RangerPolicyType.ROW_LEVEL_FILTER.getId());
        rangerPolicy.setPolicyType(RangerPolicyType.ROW_LEVEL_FILTER.getId());
        rangerPolicy.setAuditEnabled(true);

        return rangerPolicy;
    }

    /***
     * 拼装对应的行sql语句  很重要
     * @param roleRowDataPermissionDtos
     * @return
     */
    private String pingSql(List<RoleRowDataPermissionDto> roleRowDataPermissionDtos) {
        StringBuffer sb = new StringBuffer();
        //先根据字段分组
        if (CollectionUtil.isEmpty(roleRowDataPermissionDtos)) {
            return sb.toString();
        }
        Map<String, List<RoleRowDataPermissionDto>> fieldNameMap = null;
        try {
            fieldNameMap = roleRowDataPermissionDtos.stream().collect(Collectors.groupingBy(RoleRowDataPermissionDto::getFieldName));
        } catch (Exception e) {
            log.error("配置的字段名称为null,error：{}", e.getMessage());
        }
        if (CollectionUtil.isEmpty(fieldNameMap)) {
            return sb.toString();
        }
        for (Map.Entry<String, List<RoleRowDataPermissionDto>> entery : fieldNameMap.entrySet()) {
            if (CollectionUtil.isNotEmpty(entery.getValue())) {
                String sql = "";
                Map<String, List<RoleRowDataPermissionDto>> typeMap = entery.getValue().stream().collect(Collectors.groupingBy(RoleRowDataPermissionDto::getSqlType));
                if (CollectionUtil.isEmpty(typeMap)) {
                    continue;
                }
                for (Map.Entry<String, List<RoleRowDataPermissionDto>> entery1 : typeMap.entrySet()) {
                    if (CollectionUtil.isNotEmpty(entery1.getValue())) {
                        String reSql = sqlCalData(entery.getKey(), entery1.getKey(), entery1.getValue().get(0).getFieldType(), entery1.getValue());
                        if (StringUtils.isNotEmpty(reSql)) {
                            sql = sql + reSql + " and ";
                        }
                    }
                }
                if (StringUtils.isNotEmpty(sql)) {
                    sql = sql.substring(0, sql.length() - 5);
                    sb.append(sql).append(" and ");
                }
            }
        }
        if (StringUtils.isNotEmpty(sb.toString())) {
            return sb.toString().substring(0, sb.toString().length() - 5);
        }
        return sb.toString();
    }

    private String sqlCalData(String cloumn, String sqlType, String fieldType, List<RoleRowDataPermissionDto> roleRowDataPermissionDtos) {
        String sql = "";
        if (CollectionUtil.isEmpty(roleRowDataPermissionDtos)) {
            return sql;
        }

        //属于
        if (Objects.equals(SqlPressionParamType.EQ.getName(), sqlType)) {
            sql = " " + cloumn + " IN(";
            if (sqlTypeList.contains(fieldType)) {
                for (RoleRowDataPermissionDto roleRowDataPermissionDto : roleRowDataPermissionDtos) {
                    sql = sql + roleRowDataPermissionDto.getCalValue() + ",";
                }
            } else {
                for (RoleRowDataPermissionDto roleRowDataPermissionDto : roleRowDataPermissionDtos) {
                    sql = sql + "'" + roleRowDataPermissionDto.getCalValue() + "'" + ",";
                }
            }
            sql = sql.substring(0, sql.length() - 1) + ")";
        }
        //包含
        if (Objects.equals(SqlPressionParamType.IN.getName(), sqlType)) {
            //目前只能配置一个行规则
            sql = " " + cloumn + " like ( '%" + roleRowDataPermissionDtos.get(0).getCalValue() + "%' )";
        }

        //不属于
        if (Objects.equals(SqlPressionParamType.NOT_EQ.getName(), sqlType)) {
            sql = " " + cloumn + " NOT IN(";
            if (sqlTypeList.contains(fieldType)) {
                for (RoleRowDataPermissionDto roleRowDataPermissionDto : roleRowDataPermissionDtos) {
                    sql = sql + roleRowDataPermissionDto.getCalValue() + ",";
                }
            } else {
                for (RoleRowDataPermissionDto roleRowDataPermissionDto : roleRowDataPermissionDtos) {
                    sql = sql + "'" + roleRowDataPermissionDto.getCalValue() + "'" + ",";
                }
            }
            sql = sql.substring(0, sql.length() - 1) + ")";
        }
        //不包含
        if (Objects.equals(SqlPressionParamType.NOT_IN.getName(), sqlType)) {
            //目前只能配置一个行规则
            sql = " " + cloumn + "  not like ( '%" + roleRowDataPermissionDtos.get(0).getCalValue() + "%' )";
        }

        //为空
        if (Objects.equals(SqlPressionParamType.NULL.getName(), sqlType)) {
            sql = " " + cloumn + " IS NULL OR length(" + cloumn + ") = 0 ";
        }

        if (Objects.equals(SqlPressionParamType.NOT_NULL.getName(), sqlType)) {
            sql = " " + cloumn + " IS NOT NULL and length(" + cloumn + ") != 0 ";
        }

        if (Objects.equals(SqlPressionParamType.STAET_IS.getName(), sqlType)) {
            //目前只能配置一个行规则
            sql = " " + cloumn + " like ( '" + roleRowDataPermissionDtos.get(0).getCalValue() + "%' )";
        }

        if (Objects.equals(SqlPressionParamType.START_NOT.getName(), sqlType)) {
            //目前只能配置一个行规则
            sql = " " + cloumn + " not like ( '" + roleRowDataPermissionDtos.get(0).getCalValue() + "%' )";
        }

        if (Objects.equals(SqlPressionParamType.END_IS.getName(), sqlType)) {
            //目前只能配置一个行规则
            sql = " " + cloumn + " like ( '%" + roleRowDataPermissionDtos.get(0).getCalValue() + "' )";
        }

        if (Objects.equals(SqlPressionParamType.END_NOT.getName(), sqlType)) {
            //目前只能配置一个行规则
            sql = " " + cloumn + " not like ( '%" + roleRowDataPermissionDtos.get(0).getCalValue() + "' )";
        }

        return sql;
    }
    @Override
    public void deleteRangerGroup(String groupName) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        RangerGroup rangerGroup = getRangerGroup(groupName);
        if (rangerGroup!=null){
            //强制删除
            String url = rangerConfig.getResturl() + RangerRestApi.DELETE_GROUP_BY_ID + rangerGroup.getId() + "?forceDelete=true";
            WebResource webResource = client.resource(url);
            try {
                webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                        .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                        .delete();
            } catch (Exception e) {
                log.error(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_GROUP.getMessage() + ",error：{}", e.getMessage());
                throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_DELETE_GROUP.getMessage());
            }
        }

    }
    @Override
    public RangerGroup getRangerGroup(String groupName) {
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.GET_GROUP_BY_NAME+groupName;
        WebResource webResource = client.resource(url);
        RangerGroup entity = null;
        ClientResponse clientResponse = null;
        List<RangerGroup> rangerGroups = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .get(ClientResponse.class);
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerGroup.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_GET_GROUP.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_GET_GROUP.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        return entity;
    }

    @Override
    public RangerGroup addRangerGroup(RangerGroup rangerGroup) {
        RangerGroup rangerGroup1 =getRangerGroup(rangerGroup.getName());
        if (rangerGroup1!=null){
            log.info("ranger已经存在该用户组，不需要新增，rangerGroup信息:"+rangerGroup1);
            return rangerGroup1;
        }
        Client client = RangerRestUtil.getInstanceRangerClient(rangerConfig.getUsername(), rangerConfig.getPassword());
        String url = rangerConfig.getResturl() + RangerRestApi.CREATE_GROUP;
        WebResource webResource = client.resource(url);
        RangerGroup entity = null;
        ClientResponse clientResponse = null;
        try {
            clientResponse = webResource.accept(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .type(RangerRESTUtils.REST_EXPECTED_MIME_TYPE)
                    .post(ClientResponse.class, JSONObject.toJSONString(rangerGroup));
            if (clientResponse != null && clientResponse.getStatus() == 200) {
                entity = clientResponse.getEntity(RangerGroup.class);
            }
        } catch (Exception e) {
            log.error(DataDevelopmentBizExceptionEnum.RANGER_API_POST_GROUP.getMessage() + ",error：{}", e.getMessage());
            throw new RRException(DataDevelopmentBizExceptionEnum.RANGER_API_POST_GROUP.getMessage());
        } finally {
            if (null != clientResponse) {
                clientResponse.close();
            }
        }
        return entity;
    }

    @Override
    public void saveOrUpdateRangerPolicy(DataApplication apply) {
        if (apply != null && apply.getTableId() != null) {
            TableInfo table = tableInfoMapper.selectById(apply.getTableId());
            Set<String> dbNames = Sets.newHashSet();
            dbNames.add(table.getDbName());
            Set<String> tableNames = Sets.newHashSet();
            tableNames.add(table.getTableName());
            Set<String> columns = Sets.newHashSet();
            List<RangerPolicyItemAccess> operations = Lists.newArrayList();
            if (YesOrNoEnum.YES.getValue().equals(apply.getAll())) {
                columns.add("*");
                for (RangerPolicyItemAccessType value : RangerPolicyItemAccessType.values()) {
                    if (value.getId() <= 9) {
                        RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                        rangerPolicyItemAccess.setType(value.getName());
                        rangerPolicyItemAccess.setIsAllowed(true);
                        operations.add(rangerPolicyItemAccess);
                    }
                }
            } else {
                if (apply.getQuery().equals(YesOrNoEnum.YES.getValue())) {
                    columns.add("*");
                    RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.SELECT.getName());
                    rangerPolicyItemAccess.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess);
                    RangerPolicyItemAccess rangerPolicyItemAccess2 = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess2.setType(RangerPolicyItemAccessType.READ.getName());
                    rangerPolicyItemAccess2.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess2);
                }
                if (apply.getUpdate().equals(YesOrNoEnum.YES.getValue())) {
                    columns.add("*");
                    RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.UPDATE.getName());
                    rangerPolicyItemAccess.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess);
                    RangerPolicyItemAccess rangerPolicyItemAccess2 = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess2.setType(RangerPolicyItemAccessType.ALTER.getName());
                    rangerPolicyItemAccess2.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess2);
                    RangerPolicyItemAccess rangerPolicyItemAccess3 = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess3.setType(RangerPolicyItemAccessType.WRITE.getName());
                    rangerPolicyItemAccess3.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess3);
                }
                if (apply.getDelete().equals(YesOrNoEnum.YES.getValue())) {
                    columns.add("*");
                    RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.DROP.getName());
                    rangerPolicyItemAccess.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess);
                }
                if (CollectionUtil.isNotEmpty(apply.getTableFieldInfos())) {
                    columns.clear();
                    columns.addAll(apply.getTableFieldInfos().stream()
                            .map(TableFieldInfo::getFieldName).collect(Collectors.toSet()));
                    for (RangerPolicyItemAccessType value : RangerPolicyItemAccessType.values()) {
                        if (value.getId() <= 9) {
                            RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                            rangerPolicyItemAccess.setType(value.getName());
                            rangerPolicyItemAccess.setIsAllowed(true);
                            operations.add(rangerPolicyItemAccess);
                        }
                    }
                }
            }
            Long userId = apply.getUserId();
            if (ApplyStatusEnum.PASS.getType().equals(apply.getApplyStatus())) {
                Integer tableId = apply.getTableId();
                RangerPolicy policy = pingACCESSRangerPolicyByUser(dbNames, tableNames,
                        columns, userId, tableId, operations);
                log.info("生成策略:{}", JSONObject.toJSONString(policy));
                rangerPolicySave(policy, null, userId,apply.getTableId());
            }
            if (ApplyStatusEnum.EXPIRED.getType().equals(apply.getApplyStatus()) ||
                    ApplyStatusEnum.ROLLBACK.getType().equals(apply.getApplyStatus())) {
                rangerPolicySave(null, null, userId,apply.getTableId());
                log.info("删除策略:userId:{}", userId);
            }
        }
    }

    @Override
    public void saveOrUpdateRangerPolicy(List<TableInfoPermission> dataList) {
        if (CollectionUtil.isNotEmpty(dataList)) {
            Set<Integer> ids = dataList.stream().map(TableInfoPermission::getDataId).collect(Collectors.toSet());
            List<TableInfo> tables = tableInfoMapper.selectByIds(ids);
            //当前角色下的所有用户，以及所有父级的角色的用户
            Long roleId=dataList.get(0).getRoleId();
            Integer tenantId = ShiroUtils.getTenantId();
            List<SysRoleEntity> roleEntityList = sysRoleMapper.selectList(new QueryWrapper<SysRoleEntity>()
                    .eq(tenantId != null, "tenant_id", tenantId));
            List<Long> roleIds = RoleUtils.getParentIds(roleEntityList, roleId);
//            Set<String> users = new HashSet<>();
//            users.add("user_136");
            Set<String> users = sysUserMapper.selectUserByRoles(roleIds);

            if (CollectionUtil.isEmpty(tables)) {
                log.warn("权限b表信息为空,\n dataList :{}", JSONObject.toJSONString(dataList));
                return;
            }
            Map<Integer, TableInfo> tablesMap = new HashMap<>();
            for (TableInfo table : tables) {
                tablesMap.put(table.getId(), table);
            }
            ArrayList<HashMap<String, Object>> policyList = Lists.newArrayList();
            for (TableInfoPermission permission : dataList) {
                Set<String> dbNames = Sets.newHashSet();
                Set<String> tableNames = Sets.newHashSet();
                Set<String> columns = Sets.newHashSet();
                List<RangerPolicyItemAccess> operations = Lists.newArrayList();
                String dbName = tablesMap.get(permission.getDataId()).getDbName();
                if (permission.getQuery().equals(YesOrNoEnum.YES.getValue())) {
                    RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.SELECT.getName());
                    rangerPolicyItemAccess.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess);
                    RangerPolicyItemAccess rangerPolicyItemAccess2 = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess2.setType(RangerPolicyItemAccessType.READ.getName());
                    rangerPolicyItemAccess2.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess2);
                    dbNames.add(dbName);
                    tableNames.add(permission.getEnName());
                }
                if (permission.getUpdate().equals(YesOrNoEnum.YES.getValue())) {
                    RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.UPDATE.getName());
                    rangerPolicyItemAccess.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess);
                    RangerPolicyItemAccess rangerPolicyItemAccess2 = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess2.setType(RangerPolicyItemAccessType.ALTER.getName());
                    rangerPolicyItemAccess2.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess2);
                    RangerPolicyItemAccess rangerPolicyItemAccess3 = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess3.setType(RangerPolicyItemAccessType.WRITE.getName());
                    rangerPolicyItemAccess3.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess3);
                }
                if (YesOrNoEnum.YES.getValue().equals(permission.getWrite())) {
                    RangerPolicyItemAccess write = new RangerPolicyItemAccess();
                    write.setType(RangerPolicyItemAccessType.WRITE.getName());
                    write.setIsAllowed(true);
                    operations.add(write);
                }
                if (permission.getCreate().equals(YesOrNoEnum.YES.getValue())) {
                    RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                    rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.CREATE.getName());
                    rangerPolicyItemAccess.setIsAllowed(true);
                    operations.add(rangerPolicyItemAccess);
                }
                if (permission.getDelete().equals(YesOrNoEnum.YES.getValue())) {
                    operations.clear();
                    for (RangerPolicyItemAccessType value : RangerPolicyItemAccessType.values()) {
                        if (value.getId() <= 9) {
                            RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                            rangerPolicyItemAccess.setType(value.getName());
                            rangerPolicyItemAccess.setIsAllowed(true);
                            operations.add(rangerPolicyItemAccess);
                        }
                    }
                }
                if (CollectionUtil.isNotEmpty(permission.getTableFieldInfos())
                        && CollectionUtil.isNotEmpty(operations)
                ) {
                    Set<String> allSelect = permission.getTableFieldInfos().stream()
                            .filter(s->s.getQuery().equals(YesOrNoEnum.YES.getValue()))
                            .map(TableFieldInfoPermission::getChName).collect(Collectors.toSet());
                    List<TableFieldDto> fieldList = tableFieldInfoMapper.selectListByTableId(permission.getDataId());
                    if (allSelect.size()==fieldList.size()) {
                        columns.add("*");
                    }else{
                        columns.addAll(allSelect);
                    }
                }
                if (CollectionUtil.isNotEmpty(operations)) {
                    //操作去重
                    List<RangerPolicyItemAccess> unique = operations.stream().collect(
                            Collectors. collectingAndThen(
                                    Collectors.toCollection(() ->
                                            new TreeSet<>(Comparator.comparing(o -> o.getType() + ";" + o.getIsAllowed()))),
                                    ArrayList::new));

                    Integer createPer = tablesMap.get(permission.getDataId()).getCreatePer();
                    if (createPer != null) {
                        users.add("user_" + createPer);
                    }
                    RangerPolicy policy = pingACCESSRangerPolicyByRole(permission.getDataId(),dbNames,
                            tableNames, columns, roleId, unique,users);
                    addQueueForUpdatePolicy(policyList,policy, roleId,permission.getDataId());
                }else{
                    //rangerPolicySave(null, roleId,null,permission.getDataId());
                    addQueueForUpdatePolicy(policyList,null, roleId,permission.getDataId());
                }
            }
           updateRangerPolicy(policyList);
        }
    }

    @Override
    public void saveOrUpdateRangerPolicyByTenant(List<TenantTableInfoPermission> dataList) {
        if (CollectionUtil.isNotEmpty(dataList)) {
            Set<Integer> ids = dataList.stream().map(TenantTableInfoPermission::getDataId).collect(Collectors.toSet());
            List<TableInfo> tables = tableInfoMapper.selectByIds(ids);
            if (CollectionUtil.isEmpty(tables)) {
                log.warn("权限b表信息为空,\n dataList :{}", JSONObject.toJSONString(dataList));
                return;
            }
            Map<Integer, TableInfo> tablesMap = new HashMap<>();
            for (TableInfo table : tables) {
                tablesMap.put(table.getId(), table);
            }
            Integer tenantId=dataList.get(0).getTenantId();
            List<SysTenantUser> managers=sysTenantUserMapper.selectList(new QueryWrapper<SysTenantUser>().eq("tenant_id",tenantId).eq("tenant_type",0));
            if(CollectionUtils.isNotEmpty(managers)) {
                List<Integer> managerIds=managers.stream().map(i->i.getUserId()).collect(Collectors.toList());
                for(Integer userId:managerIds) {
                    ArrayList<HashMap<String, Object>> policyList = Lists.newArrayList();
                    for (TenantTableInfoPermission permission : dataList) {
                        Set<String> dbNames = Sets.newHashSet();
                        Set<String> tableNames = Sets.newHashSet();
                        Set<String> columns = Sets.newHashSet();
                        List<RangerPolicyItemAccess> operations = Lists.newArrayList();
                        String dbName = tablesMap.get(permission.getDataId()).getDbName();
                        if (permission.getQuery().equals(YesOrNoEnum.YES.getValue())) {
                            RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                            rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.SELECT.getName());
                            rangerPolicyItemAccess.setIsAllowed(true);
                            operations.add(rangerPolicyItemAccess);
                            RangerPolicyItemAccess rangerPolicyItemAccess2 = new RangerPolicyItemAccess();
                            rangerPolicyItemAccess2.setType(RangerPolicyItemAccessType.READ.getName());
                            rangerPolicyItemAccess2.setIsAllowed(true);
                            operations.add(rangerPolicyItemAccess2);
                            dbNames.add(dbName);
                            tableNames.add(permission.getEnName());
                        }
                        if (permission.getUpdate().equals(YesOrNoEnum.YES.getValue())) {
                            RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                            rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.UPDATE.getName());
                            rangerPolicyItemAccess.setIsAllowed(true);
                            operations.add(rangerPolicyItemAccess);
                            RangerPolicyItemAccess rangerPolicyItemAccess2 = new RangerPolicyItemAccess();
                            rangerPolicyItemAccess2.setType(RangerPolicyItemAccessType.ALTER.getName());
                            rangerPolicyItemAccess2.setIsAllowed(true);
                            operations.add(rangerPolicyItemAccess2);
                            RangerPolicyItemAccess rangerPolicyItemAccess3 = new RangerPolicyItemAccess();
                            rangerPolicyItemAccess3.setType(RangerPolicyItemAccessType.WRITE.getName());
                            rangerPolicyItemAccess3.setIsAllowed(true);
                            operations.add(rangerPolicyItemAccess3);
                        }
                        if (YesOrNoEnum.YES.getValue().equals(permission.getWrite())) {
                            RangerPolicyItemAccess write = new RangerPolicyItemAccess();
                            write.setType(RangerPolicyItemAccessType.WRITE.getName());
                            write.setIsAllowed(true);
                            operations.add(write);
                        }
                        if (permission.getDelete().equals(YesOrNoEnum.YES.getValue())) {
                            for (RangerPolicyItemAccessType value : RangerPolicyItemAccessType.values()) {
                                if (value.getId() <= 9) {
                                    RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                                    rangerPolicyItemAccess.setType(value.getName());
                                    rangerPolicyItemAccess.setIsAllowed(true);
                                    operations.add(rangerPolicyItemAccess);
                                }
                            }
                        }
                        if (permission.getCreate().equals(YesOrNoEnum.YES.getValue())) {
                            RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                            rangerPolicyItemAccess.setType(RangerPolicyItemAccessType.CREATE.getName());
                            rangerPolicyItemAccess.setIsAllowed(true);
                            operations.add(rangerPolicyItemAccess);
                        }
                        if (CollectionUtil.isNotEmpty(permission.getTableFieldInfos())
                                && CollectionUtil.isNotEmpty(operations)
                        ) {
                            Set<String> allSelect = permission.getTableFieldInfos().stream()
                                    .filter(s -> s.getQuery().equals(YesOrNoEnum.YES.getValue()))
                                    .map(TenantTableFieldInfoPermission::getChName).collect(Collectors.toSet());
                            List<TableFieldDto> fieldList = tableFieldInfoMapper.selectListByTableId(permission.getDataId());
                            if (allSelect.size() == fieldList.size()) {
                                columns.add("*");
                            } else {
                                columns.addAll(allSelect);
                            }
                        }

                        if (CollectionUtil.isNotEmpty(operations)) {
                            //操作去重
                            List<RangerPolicyItemAccess> unique = operations.stream().collect(
                                    Collectors.collectingAndThen(
                                            Collectors.toCollection(() ->
                                                    new TreeSet<>(Comparator.comparing(o -> o.getType() + ";" + o.getIsAllowed()))),
                                            ArrayList::new));

                            RangerPolicy policy = pingACCESSRangerPolicyByUser(dbNames, tableNames, columns, Long.valueOf(userId), permission.getDataId(), unique);
                            //rangerPolicySave(policy, roleId, null, permission.getDataId());
                            addQueueForUpdatePolicyByUser(policyList, policy, Long.valueOf(userId), permission.getDataId());
                        } else {
                            //rangerPolicySave(null, roleId,null,permission.getDataId());
                            addQueueForUpdatePolicyByUser(policyList, null, Long.valueOf(userId), permission.getDataId());
                        }
                    }
                    updateRangerPolicyByUser(policyList);
                }
            }
        }
    }

    private void updateRangerPolicy(ArrayList<HashMap<String, Object>> policyList) {
        myExecutor.execute(()->{
            Integer num=policyList.size();
            for (HashMap<String, Object> policyMap : policyList) {
                try {
                    num--;
                    Object policy = policyMap.get("policy");
                    Object roleId = policyMap.get("roleId");
                    Object tableId = policyMap.get("tableId");
                    if (roleId == null || tableId == null) {
                        continue;
                    }
                    if (policy != null) {
                        rangerPolicySave((RangerPolicy)policy, Long.parseLong(roleId.toString()), null, Integer.parseInt(tableId.toString()));
                    }else{
                        rangerPolicySave(null,  Long.parseLong(roleId.toString()), null, Integer.parseInt(tableId.toString()));
                    }
                } catch (Exception e) {
                    log.warn("更新策略异常:{}",e.getMessage());
                }
                log.info("待更新策略个数：{}", num);
            }
        });
    }

    private void updateRangerPolicyByUser(ArrayList<HashMap<String, Object>> policyList) {
        myExecutor.execute(()->{
            Integer num=policyList.size();
            for (HashMap<String, Object> policyMap : policyList) {
                try {
                    num--;
                    Object policy = policyMap.get("policy");
                    Object userId = policyMap.get("userId");
                    Object tableId = policyMap.get("tableId");
                    if (userId == null || tableId == null) {
                        continue;
                    }
                    if (policy != null) {
                        rangerPolicySave((RangerPolicy)policy,null, Long.parseLong(userId.toString()), Integer.parseInt(tableId.toString()));
                    }else{
                        rangerPolicySave(null,  null, Long.parseLong(userId.toString()), Integer.parseInt(tableId.toString()));
                    }
                } catch (Exception e) {
                    log.warn("更新策略异常:{}",e.getMessage());
                }
                log.info("待更新策略个数：{}", num);
            }
        });
    }

    @Override
    public void intiCreateTable(Set<String> intiCreateTable) {
        if (intiCreateTable != null) {
            myExecutor.execute(() -> {
                Integer num=intiCreateTable.size();
                for (String createTable : intiCreateTable) {
                    try {
                        String[] arr = createTable.split("\\|");
                        if (arr.length == 4) {
                            Set<String> dbNames = new HashSet<>();
                            Set<String> tableNames = new HashSet<>();
                            Set<String> columns = new HashSet<>();
                            String dbName = arr[0];
                            String tableName = arr[1];
                            String tableId_str = arr[2];
                            String userIds_str = arr[3];
                            List<RangerPolicyItemAccess> operations = Lists.newArrayList();
                            dbNames.add(dbName);
                            tableNames.add(tableName);
                            columns.add("*");
                            for (RangerPolicyItemAccessType value : RangerPolicyItemAccessType.values()) {
                                if (value.getId() <= 9) {
                                    RangerPolicyItemAccess rangerPolicyItemAccess = new RangerPolicyItemAccess();
                                    rangerPolicyItemAccess.setType(value.getName());
                                    rangerPolicyItemAccess.setIsAllowed(true);
                                    operations.add(rangerPolicyItemAccess);
                                }
                            }
                            Long userId = Long.parseLong(userIds_str);
                            Integer tableId = Integer.parseInt(tableId_str);
                            RangerPolicy policy = pingACCESSRangerPolicyByUser(dbNames, tableNames,
                                    columns, userId, tableId, operations);
                            rangerPolicySave(policy, null, userId, tableId);
                        }else{
                            log.info("初始化策略失败：{}", createTable);
                        }
                    } catch (Exception e) {
                        log.warn("初始化策略失败：{}", createTable);
                    }
                    log.info("待初始化表策略个数：{}", num);
                    num--;
                }
            });
        }
    }
    private void addQueueForUpdatePolicy( ArrayList<HashMap<String, Object>> policyList,RangerPolicy policy, Long roleId, Integer dataId) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("roleId", roleId);
        data.put("policy", policy);
        data.put("tableId", dataId);
        try {
            policyList.add(data);
        } catch (Exception e) {
            throw new RRException("当前ranger更新策略过多，请稍后再尝试");
        }
    }

    private void addQueueForUpdatePolicyByUser( ArrayList<HashMap<String, Object>> policyList,RangerPolicy policy, Long userId, Integer dataId) {
        HashMap<String, Object> data = new HashMap<>();
        data.put("userId", userId);
        data.put("policy", policy);
        data.put("tableId", dataId);
        try {
            policyList.add(data);
        } catch (Exception e) {
            throw new RRException("当前ranger更新策略过多，请稍后再尝试");
        }
    }

    @Override
    public void synUpdateRangerPolicy() {//每30秒更新一次策略

    }
    private void rangerPolicySave(RangerPolicy rangerPolicy, Long roleId,Long userId,Integer tableId){
            List<UserRangerPolicyRel> listAllUserRangerPolicyRels = userRangerPolicyRelService.list(new QueryWrapper<UserRangerPolicyRel>()
                    .eq(roleId != null, "role_id", roleId)
                    .eq(userId != null, "user_id", userId)
                    .eq(tableId != 0, "table_id", tableId)
                    //行规则特殊，自己创建
                    .eq("policy_type", RangerPolicyType.ACCESS.getId()));
            if (CollectionUtil.isNotEmpty(listAllUserRangerPolicyRels)) {
                for (UserRangerPolicyRel listAllUserRangerPolicyRel : listAllUserRangerPolicyRels) {
                    try {
                        deleteRangerPolicy(listAllUserRangerPolicyRel.getPolicyId());
                        //Thread.sleep(1000*10);
                    } catch (Exception e) {
                        log.error("删除ranger-策略异常！{}", e.getMessage());
                    }
                    userRangerPolicyRelService.removeById(listAllUserRangerPolicyRel.getId());
                }
            }
            //新增策略
            if (rangerPolicy != null) {
                List<UserRangerPolicyRel> userRangerPolicyRels = new ArrayList<>();
                RangerPolicy rangerPolicy1 = addRangerPolicy(rangerPolicy);
                if (null != rangerPolicy1) {
                    UserRangerPolicyRel userRangerPolicyRel = new UserRangerPolicyRel();
                    if (roleId != null) {
                        userRangerPolicyRel.setRoleId(roleId.intValue());
                    }
                    if (userId != null) {
                        userRangerPolicyRel.setUserId(userId);
                    }else{
                        userRangerPolicyRel.setUserId(-1L);
                    }
                    userRangerPolicyRel.setPolicyId(rangerPolicy1.getId());
                    userRangerPolicyRel.setCreateTime(DateTime.now());
                    userRangerPolicyRel.setPolicyType(rangerPolicy1.getPolicyType());
                    userRangerPolicyRel.setTableId(tableId);
                    userRangerPolicyRels.add(userRangerPolicyRel);
                }else{
                    log.error("生成策略失败{}", JSONObject.toJSONString(rangerPolicy));
                }
                //批量保存关联信息
                if (CollectionUtil.isNotEmpty(userRangerPolicyRels)) {
                    userRangerPolicyRelService.saveBatch(userRangerPolicyRels);
                }
            }
    }
    private RangerPolicy pingACCESSRangerPolicyByUser( Set<String> dbName, Set<String> tableName,Set<String> columnList, Long userId,Integer tableId,List<RangerPolicyItemAccess> operations) {
        RangerPolicy rangerPolicy = new RangerPolicy();
        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();
        //字段
        RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
        rangerPolicyResource.getValues().addAll(columnList);
        rangerPolicyResource.setIsExcludes(false);
        rangerPolicyResource.setIsRecursive(false);
        //表
        //添加一个唯一标识的表，防止对用户针对同一个表的时候的权限问题【bug】---ranger的bug【不同用户针对一个表设置相同的策略会报错】
        RangerPolicyResource rangerPolicyResourceTable = new RangerPolicyResource();
        rangerPolicyResourceTable.getValues().addAll(tableName);
        rangerPolicyResourceTable.getValues().add(UUID.randomUUID().toString());
        rangerPolicyResourceTable.setIsExcludes(false);
        rangerPolicyResourceTable.setIsRecursive(false);

        //库
        RangerPolicyResource rangerPolicyResourceDb = new RangerPolicyResource();
        rangerPolicyResourceDb.getValues().addAll(dbName);
        rangerPolicyResourceDb.setIsExcludes(false);
        rangerPolicyResourceDb.setIsRecursive(false);
        //对应的库的资源
        resourceMap.put(RangerDBType.COLUMN.getName(), rangerPolicyResource);
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResourceTable);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResourceDb);
        rangerPolicy.setResources(resourceMap);

        //通用配置
        RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
        rangerPolicyItem.getAccesses().addAll(operations);
         //rangerPolicyItem.getGroups().addAll(Arrays.asList(dataDevelopmentConfig.getUserPreffix() + userId));
        rangerPolicyItem.getUsers().add(dataDevelopmentConfig.getUserPreffix() + userId);
        rangerPolicy.getPolicyItems().add(rangerPolicyItem);

        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName(dataDevelopmentConfig.getUserPolicyPreffix() + userId + "_"+tableId+"_" + RangerPolicyType.ACCESS.getId());
        rangerPolicy.setPolicyType(RangerPolicyType.ACCESS.getId());
        rangerPolicy.setAuditEnabled(true);
        return rangerPolicy;
    }

    private RangerPolicy pingACCESSRangerPolicyByRole(Integer tableId, Set<String> dbName,
                                                      Set<String> tableName, Set<String> columnList,
                                                      Long roleId,
                                                      List<RangerPolicyItemAccess> operations, Set<String> users) {
        RangerPolicy rangerPolicy = new RangerPolicy();
        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();
        //字段
        RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
        rangerPolicyResource.getValues().addAll(columnList);
        rangerPolicyResource.setIsExcludes(false);
        rangerPolicyResource.setIsRecursive(false);
        //表
        //添加一个唯一标识的表，防止对用户针对同一个表的时候的权限问题【bug】---ranger的bug【不同用户针对一个表设置相同的策略会报错】
        RangerPolicyResource rangerPolicyResourceTable = new RangerPolicyResource();
        rangerPolicyResourceTable.getValues().addAll(tableName);
        rangerPolicyResourceTable.getValues().add(UUID.randomUUID().toString());
        rangerPolicyResourceTable.setIsExcludes(false);
        rangerPolicyResourceTable.setIsRecursive(false);

        //库
        RangerPolicyResource rangerPolicyResourceDb = new RangerPolicyResource();
        rangerPolicyResourceDb.getValues().addAll(dbName);
        rangerPolicyResourceDb.setIsExcludes(false);
        rangerPolicyResourceDb.setIsRecursive(false);
        //对应的库的资源
        resourceMap.put(RangerDBType.COLUMN.getName(), rangerPolicyResource);
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResourceTable);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResourceDb);
        rangerPolicy.setResources(resourceMap);
        //通用配置
        RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
        rangerPolicyItem.getAccesses().addAll(operations);
        rangerPolicyItem.getUsers().addAll(users);
        rangerPolicy.getPolicyItems().add(rangerPolicyItem);
        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName(dataDevelopmentConfig.getRolePolicyPreffix() + roleId + "_"+tableId +"_"+ RangerPolicyType.ACCESS.getId());
        rangerPolicy.setPolicyType(RangerPolicyType.ACCESS.getId());
        rangerPolicy.setAuditEnabled(true);
        return rangerPolicy;
    }
    private RangerPolicy pingACCESSRangerPolicy(List<String> allSelect, TableAndFieldDto tableAndFieldDto, UserAndTableDto userAndTableDto) {
        RangerPolicy rangerPolicy = new RangerPolicy();
        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();

        //字段
        RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
        rangerPolicyResource.getValues().addAll(allSelect);
        rangerPolicyResource.setIsExcludes(false);
        rangerPolicyResource.setIsRecursive(false);

        //表
        //添加一个唯一标识的表，防止对用户针对同一个表的时候的权限问题【bug】---ranger的bug【不同用户针对一个表设置相同的策略会报错】
        RangerPolicyResource rangerPolicyResourceTable = new RangerPolicyResource();
        rangerPolicyResourceTable.getValues().add(tableAndFieldDto.getTableName());
        rangerPolicyResourceTable.getValues().add(UUID.randomUUID().toString());
        rangerPolicyResourceTable.setIsExcludes(false);
        rangerPolicyResourceTable.setIsRecursive(false);

        //库
        RangerPolicyResource rangerPolicyResourceDb = new RangerPolicyResource();
        rangerPolicyResourceDb.getValues().add(tableAndFieldDto.getDbName());
        rangerPolicyResourceDb.setIsExcludes(false);
        rangerPolicyResourceDb.setIsRecursive(false);
        //对应的库的资源
        resourceMap.put(RangerDBType.COLUMN.getName(), rangerPolicyResource);
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResourceTable);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResourceDb);
        rangerPolicy.setResources(resourceMap);

        //通用配置
        RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
        rangerPolicyItem.getAccesses().addAll(listRangerPolicyItemAccess());
        rangerPolicyItem.getUsers().addAll(Arrays.asList(dataDevelopmentConfig.getUserPreffix() + userAndTableDto.getUserId()));
        rangerPolicy.getPolicyItems().add(rangerPolicyItem);

        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName(dataDevelopmentConfig.getUserPolicyPreffix() + userAndTableDto.getUserId() + "_" + tableAndFieldDto.getTableInfoId() + "_" + RangerPolicyType.ACCESS.getId());
        rangerPolicy.setPolicyType(RangerPolicyType.ACCESS.getId());
        rangerPolicy.setAuditEnabled(true);

        return rangerPolicy;
    }




    /***
     * 生成数据脱敏的策略
     * @param tableId  表id
     * @param tableFieldId 字段id
     * @param allRoleRowPerssionList 权限集合
     * @return
     */
    private RangerPolicy pingMaskRangerPolicy(Integer tableId, Integer tableFieldId, List<UserAndRoleRowPerssionDto> allRoleRowPerssionList) {

        //生成对应表下所有的用户的行规则策略
        TableInfo tableInfo = tableInfoService.getById(tableId);
        if (null == tableInfo) {
            return null;
        }
        TableFieldInfo tableFieldInfo = tableFieldInfoMapper.selectById(tableFieldId);
        if(null == tableFieldInfo){
            return null;
        }
        //查询到对应的字段的规则，
        TableFieldMaskRules tableFieldMaskRules = tableFieldMaskRulesMapper.selectOne(new QueryWrapper<TableFieldMaskRules>().eq("table_field_id", tableFieldId));
        if(null == tableFieldMaskRules){
            return null;
        }
        Set<Long> needMaskUserIds = new HashSet<>();
        for (UserAndRoleRowPerssionDto userAndRoleRowPerssionDto : allRoleRowPerssionList) {
            //根据行规则进行拼装对应的sql语句
            if (null == userAndRoleRowPerssionDto ||
                    //CollectionUtil.isEmpty(userAndRoleRowPerssionDto.getAllPermissionList()) ||
                    CollectionUtil.isEmpty(userAndRoleRowPerssionDto.getAllUserList())) {
                continue;
            }
            //验证库表等级----不满足库表权限，则返回true  相反则false，还有白名单
//            if (cheackTableAndDbAndClonm(userAndRoleRowPerssionDto.getRoleId(), tableId, tableFieldId)) {
//                continue;
//            }
            List<SysUserRoleEntity> allUserList = userAndRoleRowPerssionDto.getAllUserList();
            if (CollectionUtil.isEmpty(allUserList)) {
                continue;
            }
            allUserList.forEach(sysUserRoleEntity -> {
                needMaskUserIds.add(sysUserRoleEntity.getUserId());
            });
        }

        if(CollectionUtil.isEmpty(needMaskUserIds)){
            return null;
        }
        //拼装对应的策略
        RangerPolicy rangerPolicy = pingMaskRangerPolicyData(tableInfo,tableFieldInfo,tableFieldMaskRules,needMaskUserIds);
        if (CollectionUtil.isEmpty(rangerPolicy.getDataMaskPolicyItems())) {
            return null;
        }
        return rangerPolicy;
    }

    private RangerPolicy pingMaskRangerPolicyData(TableInfo tableInfo, TableFieldInfo tableFieldInfo,TableFieldMaskRules tableFieldMaskRules, Set<Long> needMaskUserIds) {
        RangerPolicy rangerPolicy = new RangerPolicy();
        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();

        //字段
        RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
        rangerPolicyResource.getValues().add(tableFieldInfo.getFieldName());
        rangerPolicyResource.setIsExcludes(false);
        rangerPolicyResource.setIsRecursive(false);

        //表
        RangerPolicyResource rangerPolicyResourceTable = new RangerPolicyResource();
        rangerPolicyResourceTable.getValues().add(tableInfo.getTableName());
        rangerPolicyResourceTable.setIsExcludes(false);
        rangerPolicyResourceTable.setIsRecursive(false);

        //库
        RangerPolicyResource rangerPolicyResourceDb = new RangerPolicyResource();
        rangerPolicyResourceDb.getValues().add(tableInfo.getDbName());
        rangerPolicyResourceDb.setIsExcludes(false);
        rangerPolicyResourceDb.setIsRecursive(false);
        //对应的库的资源
        resourceMap.put(RangerDBType.COLUMN.getName(), rangerPolicyResource);
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResourceTable);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResourceDb);
        rangerPolicy.setResources(resourceMap);

        //通用配置
        RangerMaskPolicyItem rangerMaskPolicyItem = new RangerMaskPolicyItem();
        rangerMaskPolicyItem.getAccesses().add(new RangerPolicyItemAccess(RangerPolicyItemAccessType.SELECT.getName(), true));
        Set<String> userIds = new HashSet<>();
        needMaskUserIds.forEach(str ->{
           String rangUserName= dataDevelopmentConfig.getUserPreffix() + str;
            RangerUser rangerUser = getRangerUser(rangUserName);
            if (rangerUser != null) {
                userIds.add(rangUserName);
            }else{
                log.warn("ranger中不存在用户{}",rangUserName);
            }
        });

        rangerMaskPolicyItem.getUsers().addAll(userIds);
        RangerDataMaskInfo rangerDataMaskInfo = new RangerDataMaskInfo();
        Integer maskType = tableFieldMaskRules.getMaskType();
        String valueExpr = "";
        if(maskType >= RangerMaskType.MASK_CUSTOM.getId()){
            maskType = RangerMaskType.MASK_CUSTOM.getId();
            //查询到内置的udf函数名字
            CusMaskUdf cusMaskUdf = cusMaskUdfMapper.selectById(tableFieldMaskRules.getMaskType());
            if(null != cusMaskUdf){
                valueExpr = packCusUdfStr(cusMaskUdf.getUdfFuncName(),cusMaskUdf.getId(),tableFieldMaskRules.getMaskValue());
            }
        }
        rangerDataMaskInfo.setDataMaskType(RangerMaskType.ObjOf(maskType).getName());
        rangerDataMaskInfo.setValueExpr(valueExpr);
        rangerMaskPolicyItem.setDataMaskInfo(rangerDataMaskInfo);
        rangerPolicy.getDataMaskPolicyItems().add(rangerMaskPolicyItem);


        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName(dataDevelopmentConfig.getMaskPolicyPreffix() + tableFieldInfo.getId()+"_" + tableInfo.getId() + "_" + RangerPolicyType.MASKING.getId());
        rangerPolicy.setPolicyType(RangerPolicyType.MASKING.getId());
        rangerPolicy.setAuditEnabled(true);

        return rangerPolicy;
    }

    private String packCusUdfStr(String udfFuncName, Integer maskType, String maskValue) {
        if(maskType == 8 || maskType == 9){
            int start = 0 ,end = 0;
            try {
                String[] split = maskValue.split(",");
                if(split.length >= 2){
                    start = Integer.valueOf(split[0]);
                    end = Integer.valueOf(split[1]);
                }
            }catch (Exception e){

            }
            return "default."+ udfFuncName + "({col},"+ start +", "+end+")";
        }
        return "default." + udfFuncName + "({col})";
    }

    private boolean cheackTableAndDbAndClonm(Long roleId, Integer tableId, Integer tableFieldId) {
        //查询角色配置的库表权限
        if (null == roleId || null == tableId) {
            return true;
        }
        //还得验证表的白名单
        Set<Integer> dbIds = new HashSet<>();
        Set<Integer> tableLevelIds = new HashSet<>();
        Set<Integer> colunmIds = new HashSet<>();
        tableInfoService.initDbAndTableAndColum(dbIds, tableLevelIds, colunmIds, roleId);
        if (CollectionUtil.isEmpty(dbIds) || CollectionUtil.isEmpty(tableLevelIds) || CollectionUtil.isEmpty(colunmIds)) {
            return true;
        }
        //查询表对应的库表权限
        //获取到对应的全局默认配置
        AllPermissionConfig allPermissionConfig = allPermissionConfigMapper.selectOneByType(PressionParamType.TABLE.getId());
        AllPermissionConfig allPermissionConfigCulonm = allPermissionConfigMapper.selectOneByType(PressionParamType.CULONM.getId());
        //拼装对应的表和表的字段信息
        TableAndFieldDto tableAndFieldDto = tableInfoMapper.getTableByIdToTable(tableId);
        if (null != tableAndFieldDto) {
            //验证配置的白名单
            if (StringUtils.isNotEmpty(tableAndFieldDto.getTableMaskWhiteList())) {
                if(cheackWhiteLIst(tableLevelIds,tableAndFieldDto.getTableMaskWhiteList())){
                    return true;
                }
            }
            //表等级
            if (null == tableAndFieldDto.getTableLevelId()) {
                if (null != allPermissionConfig) {
                    tableAndFieldDto.setTableLevelId(allPermissionConfig.getDataPermissionId());
                }
            }
            //表的字段
            TableFieldInfo tableFieldInfo = tableFieldInfoMapper.selectById(tableFieldId);
            if (null == tableFieldInfo) {
                return true;
            }
            TableFieldDto tableFieldDto = new TableFieldDto();
            tableFieldDto.setFieldName(tableFieldInfo.getFieldName());
            tableFieldDto.setColcumLevelId(tableFieldInfo.getColcumLevelId());

            if (null == tableFieldInfo.getColcumLevelId()) {
                if (null != allPermissionConfigCulonm) {
                    tableFieldDto.setColcumLevelId(allPermissionConfigCulonm.getDataPermissionId());
                }
            }
            List<TableFieldDto> restTableFieldList = Arrays.asList(tableFieldDto);
            tableAndFieldDto.setAllFieldList(restTableFieldList);
        }
        if (null == tableAndFieldDto) {
            return true;
        }
        //验证库表和字段等级
        if (dbIds.contains(tableAndFieldDto.getDbId())) {
            if (tableLevelIds.contains(tableAndFieldDto.getTableLevelId())) {
                if (colunmIds.contains(tableAndFieldDto.getAllFieldList().get(0).getColcumLevelId())) {
                    return false;
                }
            }
        }
        return true;
    }

    /***
     * 白名单是否包含，如果包含，则不需要配置这个策略
     * @param tableLevelIds
     * @param tableMaskWhiteList
     * @return  true:包含白名单，则不需要配置脱敏规则  false:不包含白名单，需要配置规则
     */
    private Boolean cheackWhiteLIst(Set<Integer> tableLevelIds, String tableMaskWhiteList) {
        if(StringUtils.isEmpty(tableMaskWhiteList)){
            return false;
        }
        try {
            String[] split = tableMaskWhiteList.split(",");
            for (String s : split) {
                if(tableLevelIds.contains(Integer.valueOf(s))){
                    return true;
                }
            }
        }catch (Exception e){
            log.error("截取白名单:{}数据异常，error:{}",tableMaskWhiteList,e.getMessage());
        }
        return false;
    }


    private RangerPolicy pingACCESSRangerPolicy(Long userId, List<String> allSelect, Integer tableId, String tableName, String dbName) {
        RangerPolicy rangerPolicy = new RangerPolicy();
        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();

        //字段
        RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
        rangerPolicyResource.getValues().addAll(allSelect);
        rangerPolicyResource.setIsExcludes(false);
        rangerPolicyResource.setIsRecursive(false);

        //表
        //添加一个唯一标识的表，防止对用户针对同一个表的时候的权限问题【bug】---ranger的bug【不同用户针对一个表设置相同的策略会报错】
        RangerPolicyResource rangerPolicyResourceTable = new RangerPolicyResource();
        rangerPolicyResourceTable.getValues().add(tableName);
        rangerPolicyResourceTable.getValues().add(UUID.randomUUID().toString());
        rangerPolicyResourceTable.setIsExcludes(false);
        rangerPolicyResourceTable.setIsRecursive(false);

        //库
        RangerPolicyResource rangerPolicyResourceDb = new RangerPolicyResource();
        rangerPolicyResourceDb.getValues().add(dbName);
        rangerPolicyResourceDb.setIsExcludes(false);
        rangerPolicyResourceDb.setIsRecursive(false);
        //对应的库的资源
        resourceMap.put(RangerDBType.COLUMN.getName(), rangerPolicyResource);
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResourceTable);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResourceDb);
        rangerPolicy.setResources(resourceMap);

        //通用配置
        RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
        rangerPolicyItem.getAccesses().addAll(listRangerPolicyItemAccess());
        rangerPolicyItem.getUsers().addAll(Arrays.asList(dataDevelopmentConfig.getUserPreffix() + userId));
        rangerPolicy.getPolicyItems().add(rangerPolicyItem);

        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName(dataDevelopmentConfig.getUserPolicyPreffix() + userId + "_" + tableId + "_" + RangerPolicyType.ACCESS.getId());
        rangerPolicy.setPolicyType(RangerPolicyType.ACCESS.getId());
        rangerPolicy.setAuditEnabled(true);

        return rangerPolicy;
    }


    private List<String> listAllColumn(List<TableFieldDto> allFieldList, Set<Integer> allConlumIds) {
        List<String> resList = new ArrayList<>();
        if (CollectionUtil.isEmpty(allConlumIds) || CollectionUtil.isEmpty(allFieldList)) {
            return resList;
        }
        for (TableFieldDto tableFieldDto : allFieldList) {
            if (allConlumIds.contains(tableFieldDto.getColcumLevelId())) {
                resList.add(tableFieldDto.getFieldName());
            }
        }
        //如果是全部字段都有权限，则用*号表示
        //解决不能insert的权限报错的问题
        if (allFieldList.size() == resList.size()) {
            resList.clear();
            resList.add("*");
        }
        return resList;
    }

    /***
     * 包装排除所有的权限的方法
     * @param rangerPolicyDto
     */
    private void packExcludeAllData(RangerPolicyDto rangerPolicyDto, Long userId) {
        List<RangerPolicy> rangerPolicies = new ArrayList<>();
        RangerPolicy rangerPolicy = new RangerPolicy();

        Map<String, RangerPolicyResource> resourceMap = new HashMap<>();
        RangerPolicyResource rangerPolicyResource = new RangerPolicyResource();
        rangerPolicyResource.getValues().add("ABCDDCBA");
        rangerPolicyResource.setIsExcludes(false);
        rangerPolicyResource.setIsRecursive(false);
        resourceMap.put(RangerDBType.COLUMN.getName(), rangerPolicyResource);

        RangerPolicyResource rangerPolicyResource2 = new RangerPolicyResource();
        rangerPolicyResource2.getValues().add("ABCDDCBA");
        rangerPolicyResource2.setIsExcludes(false);
        rangerPolicyResource2.setIsRecursive(false);
        resourceMap.put(RangerDBType.DATABASE.getName(), rangerPolicyResource2);

        //添加一个唯一标识的表，防止对用户针对同一个表的时候的权限问题【bug】---ranger的bug【不同用户针对一个表设置相同的策略会报错】
        RangerPolicyResource rangerPolicyResource1 = new RangerPolicyResource();
        rangerPolicyResource1.getValues().add("ABCDDCBA");
        rangerPolicyResource1.getValues().add(UUID.randomUUID().toString());
        rangerPolicyResource1.setIsExcludes(false);
        rangerPolicyResource1.setIsRecursive(false);
        resourceMap.put(RangerDBType.TABLE.getName(), rangerPolicyResource1);
        rangerPolicy.setResources(resourceMap);

        RangerPolicyItem rangerPolicyItem = new RangerPolicyItem();
        rangerPolicyItem.getAccesses().addAll(listRangerPolicyItemAccess());
        rangerPolicyItem.getUsers().addAll(Arrays.asList(rangerPolicyDto.getRangerUserName()));
        rangerPolicy.getPolicyItems().add(rangerPolicyItem);

        rangerPolicy.setEnabled(true);
        rangerPolicy.setService(dataDevelopmentConfig.getHiveService());
        rangerPolicy.setName(dataDevelopmentConfig.getUserPolicyPreffix() + userId + "_0_" + RangerPolicyType.ACCESS.getId());
        rangerPolicy.setPolicyType(RangerPolicyType.ACCESS.getId());
        rangerPolicy.setAuditEnabled(true);
        rangerPolicies.add(rangerPolicy);
        rangerPolicyDto.setRangerPolicies(rangerPolicies);
    }


}
